﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Catering_Rules;
using Catering_Model;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Collections;

namespace Catering.View_Operaction
{
    public class View_Monitor
    {
        private Catering_Rules.DataViewForReport dvf = new DataViewForReport();
        private Catering_Rules.ClassInfo classRules = new Catering_Rules.ClassInfo();
        private Catering_Rules.InDbInfo inDbRules = new Catering_Rules.InDbInfo();

        #region 日常监控
        public DataTable GetRiChangJianKongByMonth(string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.Type_CalDataTable dt = new CMS_CateringDataSet.Type_CalDataTable();
            for (int i = 1; i <= 31; i++)
            {
                DataRow dr1, dr2, dr3, dr4, dr5, dr6, dr7, dr8, dr9, dr10;
                GetDRFromRiChangJianKong(new Random().Next(10) * DateTime.Now.Month, "谷物", i, out dr1);
                GetDRFromRiChangJianKong(new Random().Next(13) * DateTime.Now.Month, "禽肉", i, out dr2);
                GetDRFromRiChangJianKong(new Random().Next(15) * DateTime.Now.Month, "鱼虾", i, out dr3);
                GetDRFromRiChangJianKong(new Random().Next(17) * DateTime.Now.Month, "蔬菜", i, out dr4);
                GetDRFromRiChangJianKong(new Random().Next(20) * DateTime.Now.Month, "水果", i, out dr5);
                GetDRFromRiChangJianKong(5, "谷物（标准）", i, out dr6);
                GetDRFromRiChangJianKong(3, "禽肉（标准）", i, out dr7);
                GetDRFromRiChangJianKong(6, "鱼虾（标准）", i, out dr8);
                GetDRFromRiChangJianKong(9, "蔬菜（标准）", i, out dr9);
                GetDRFromRiChangJianKong(12, "水果（标准）", i, out dr10);
                dt.Rows.Add(dr1.ItemArray); dt.Rows.Add(dr2.ItemArray); dt.Rows.Add(dr3.ItemArray);
                dt.Rows.Add(dr4.ItemArray); dt.Rows.Add(dr5.ItemArray); dt.Rows.Add(dr6.ItemArray);
                dt.Rows.Add(dr7.ItemArray); dt.Rows.Add(dr8.ItemArray); dt.Rows.Add(dr9.ItemArray);
                dt.Rows.Add(dr10.ItemArray);
            }
            return dt;

        }
        private void GetDRFromRiChangJianKong(int num, string FoodType, int Month, out DataRow dr)
        {
            dr = new CMS_CateringDataSet.Type_CalDataTable().NewRow();
            dr["Month"] = Month;
            dr["FoodType"] = FoodType;
            dr["Numbers"] = num;
        }


        public DataTable GetRiChangJianKongForList(string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.Type_CalDataTable dt = new CMS_CateringDataSet.Type_CalDataTable();
            DataRow dr1 = dt.NewRow(), dr2 = dt.NewRow(), dr3 = dt.NewRow(), dr4 = dt.NewRow(), dr5 = dt.NewRow();
            dr1["FoodType"] = "谷物";
            dr2["FoodType"] = "禽肉";
            dr3["FoodType"] = "鱼虾";
            dr4["FoodType"] = "蔬菜";
            dr5["FoodType"] = "水果";
            dr1["Numbers"] = new Random().Next(300) * DateTime.Now.Month;
            dr2["Numbers"] = new Random().Next(300) * DateTime.Now.Month;
            dr3["Numbers"] = new Random().Next(300) * DateTime.Now.Month;
            dr4["Numbers"] = new Random().Next(300) * DateTime.Now.Month;
            dr5["Numbers"] = new Random().Next(300) * DateTime.Now.Month;
            dr1["Month"] = new Random().Next(280) * DateTime.Now.Month;
            dr2["Month"] = new Random().Next(280) * DateTime.Now.Month;
            dr3["Month"] = new Random().Next(280) * DateTime.Now.Month;
            dr4["Month"] = new Random().Next(280) * DateTime.Now.Month;
            dr5["Month"] = new Random().Next(280) * DateTime.Now.Month;
            dr1["EatNumber"] = new Random().Next(200);
            dr2["EatNumber"] = new Random().Next(200);
            dr3["EatNumber"] = new Random().Next(200);
            dr4["EatNumber"] = new Random().Next(200);
            dr5["EatNumber"] = new Random().Next(200);
            dr1["OutDate"] = (FormatDecimal(dr1["Numbers"]) / FormatDecimal(dr1["EatNumber"]));
            dr2["OutDate"] = (FormatDecimal(dr2["Numbers"]) / FormatDecimal(dr2["EatNumber"]));
            dr3["OutDate"] = (FormatDecimal(dr3["Numbers"]) / FormatDecimal(dr3["EatNumber"]));
            dr4["OutDate"] = (FormatDecimal(dr4["Numbers"]) / FormatDecimal(dr4["EatNumber"]));
            dr5["OutDate"] = (FormatDecimal(dr5["Numbers"]) / FormatDecimal(dr5["EatNumber"]));

            dt.Rows.Add(dr1); dt.Rows.Add(dr2); dt.Rows.Add(dr3); dt.Rows.Add(dr4); dt.Rows.Add(dr5);
            return dt;

        }
        #endregion

        #region 消耗分类汇总数据
        /// <summary>
        /// 消耗分类汇总数据 年条件
        /// </summary>
        /// <returns></returns>
        public CMS_CateringDataSet.View_XiaoHaoSumDataTable GetViewSumToDataSet(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE, string TITLE2)
        {
            CMS_CateringDataSet.View_XiaoHaoSumDataTable dt = new CMS_CateringDataSet.View_XiaoHaoSumDataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetXiaohao_TypeSum(strarealist, AREAID, CLASSID, DTIME);//4.获取当前出库数据
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["TypeName"] = dttemp.Rows[i]["RTName"];
                    dr["Name"] = dttemp.Rows[i]["RInfoName"];
                    dr["January"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["1"]) / 1000, 2, 3);
                    dr["February"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["2"]) / 1000, 2, 3);
                    dr["March"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["3"]) / 1000, 2, 3);
                    dr["April"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["4"]) / 1000, 2, 3);
                    dr["May"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["5"]) / 1000, 2, 3);
                    dr["June"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["6"]) / 1000, 2, 3);
                    dr["July"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["7"]) / 1000, 2, 3);
                    dr["August"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["8"]) / 1000, 2, 3);
                    dr["September"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["9"]) / 1000, 2, 3);
                    dr["October"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["10"]) / 1000, 2, 3);
                    dr["November"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["11"]) / 1000, 2, 3);
                    dr["December"] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i]["12"]) / 1000, 2, 3);
                    dr["Inventory"] = GetXiaohaoResult(dttemp.Rows[i]);
                    dr["Price"] = TITLE;
                    dr["RawId"] = dttemp.Rows[i]["RTID"];
                    dr["CName"] = TITLE2;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        private decimal GetXiaohaoResultForMonth(DataRow dr)
        {
            decimal count = 0;
            for (int i = 1; i <= 31; i++)
            {
                count += Toolss.GetMathResult(FormatDecimal(dr[i.ToString()]) / 1000, 2, 3);
            }
            return count;
        }

        private decimal GetXiaohaoResult(DataRow dr)
        {
            decimal count = 0;
            object[] obj = new object[] {
                dr["1"],
                dr["2"],
                dr["3"],
                dr["4"],
                dr["5"],
                dr["6"],
                dr["7"],
                dr["8"],
                dr["9"],
                dr["10"],
                dr["11"],
                dr["12"]
            };
            for (int i = 0; i < obj.Count(); i++)
            {
                count += Toolss.GetMathResult(FormatDecimal(obj[i]) / 1000, 2, 3);
            }
            return count;
        }

        /// <summary>
        /// 消耗分类汇总数据 月条件
        /// </summary>
        /// <returns></returns>
        public CMS_CateringDataSet.View_XiaoHaoSumForMonthDataTable GetViewSumToDataTableForMonth(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.View_XiaoHaoSumForMonthDataTable dt = new CMS_CateringDataSet.View_XiaoHaoSumForMonthDataTable();
            string strArealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetXiaohao_TypeSumForMonth(strArealist, AREAID, CLASSID, DTIME);//4.获取当前出库数据
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["TypeName"] = dttemp.Rows[i]["RTName"];
                    dr["Name"] = dttemp.Rows[i]["RInfoName"];
                    for (int ii = 1; ii <= 31; ii++)
                    {
                        dr["D" + ii.ToString()] = Toolss.GetMathResult(FormatDecimal(dttemp.Rows[i][ii.ToString()]) / 1000, 2, 1);
                    }
                    dr["Inventory"] = GetXiaohaoResultForMonth(dttemp.Rows[i]);
                    dr["Price"] = TITLE;
                    dr["RawId"] = dttemp.Rows[i]["RTID"];
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion

        #region 消耗计算
        /// <summary>
        /// 消耗计算 年条件
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="year"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetXiaohao_Cal(string pid, string AreaID, string ClassID, string year, string Title)
        {
            CMS_CateringDataSet.View_1DataTable dt = new CMS_CateringDataSet.View_1DataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttempRuKu = dvf.GetXiaohao_CalRuKu(strarealist, AreaID, ClassID, year);//4.获取当前出库数据
            DataTable dttempChuKu = dvf.GetViewFromRecordsXiaoHaoCal(strarealist, AreaID, ClassID, DateTime.Parse(year + "-01-01 00:00:11"));
            List<Catering_Model.Units> listUnits = new Catering_Rules.Units().GetModelList("");
            List<Catering_Model.RawMaterialsInfo> listRawInfo = new Catering_Rules.RawMaterialsInfo().GetModelList("");
            string tName = "";
            if (!string.IsNullOrEmpty(ClassID) && !ClassID.Equals("-10"))
            {
                tName = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(ClassID)).CName;
            }
            else if (!string.IsNullOrEmpty(AreaID) && !AreaID.Equals("-10"))
            {
                tName = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(AreaID)).AreaName;
            }
            if (dttempChuKu != null && dttempChuKu.Rows.Count > 0 && dttempRuKu != null && dttempRuKu.Rows.Count > 0)
            {
                for (int i = 0; i < dttempRuKu.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Number"] = Title;
                    if (!string.IsNullOrEmpty(tName))
                    {
                        dr["mymonth"] = tName;
                    }
                    decimal indb = 0;
                    dr["RawId"] = dttempRuKu.Rows[i]["RTID"];
                    dr["TypeName"] = dttempRuKu.Rows[i]["RTName"];
                    dr["RawName"] = dttempRuKu.Rows[i]["RawName"];
                    dr["Name"] = dttempRuKu.Rows[i]["RInfoName"];
                    dr["RawMaterialid"] = dttempRuKu.Rows[i]["RAWID"];
                    dr["Januaryr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["1r"]) / 1000, 2, 3);
                    dr["Februaryr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["2r"]) / 1000, 2, 3);
                    dr["Marchr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["3r"]) / 1000, 2, 3);
                    dr["Aprilr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["4r"]) / 1000, 2, 3);
                    dr["Mayr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["5r"]) / 1000, 2, 3);
                    dr["Juner"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["6r"]) / 1000, 2, 3);
                    dr["Julyr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["7r"]) / 1000, 2, 3);
                    dr["Augustr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["8r"]) / 1000, 2, 3);
                    dr["Septemberr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["9r"]) / 1000, 2, 3);
                    dr["Octoberr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["10r"]) / 1000, 2, 3);
                    dr["Novemberr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["11r"]) / 1000, 2, 3);
                    dr["Decemberr"] = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["12r"]) / 1000, 2, 3);
                    indb = GetInDBResult(dttempRuKu.Rows[i]);
                    dr["id"] = indb;
                    #region 绑定单位
                    string guige = "";
                    Catering_Model.E_RawMaterial model = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(dr["RawMaterialid"]));
                    string balace = dvf.GetInventory(model.id.ToString(), Convert.ToInt32(year), ClassID); //库存盘点
                    if (!string.IsNullOrEmpty(balace))
                    {
                        bool iskg = true;
                        decimal guige01 = 1;
                        //入库单位
                        string danwei = new Catering_Rules.Units().GetModel(Convert.ToInt32(model.temp2)).UName;
                        decimal UtilKg = Toolss.GetSpecifications(Convert.ToInt32(dr["RawMaterialid"]), danwei, out iskg);
                        guige01 = iskg ? 1000 : 1;
                        decimal weight = Toolss.GetMathResult(UtilKg * Convert.ToDecimal(balace), 2, 3);
                        dr["temp1"] = Toolss.GetMathResult(weight * guige01 / 1000, 2, 3);
                    }
                    else
                    {
                        dr["temp1"] = 0;
                    }

                    try
                    {
                        DataTable dt_RawMaterial_Unit = new Catering_Rules.RawMaterial_Unit().GetList("RawMaterialID=" + dttempRuKu.Rows[i]["RAWID"].ToString()).Tables[0];

                        if (dt_RawMaterial_Unit != null && dt_RawMaterial_Unit.Rows.Count > 0)
                        {
                            if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[0]["Number"].ToString()))
                            {
                                guige += dt_RawMaterial_Unit.Rows[0]["Number"].ToString();
                                guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[0]["UnitID"])).First().UName + "*";
                                if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[1]["Number"].ToString()))
                                {
                                    guige += dt_RawMaterial_Unit.Rows[1]["Number"].ToString();
                                    guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[1]["UnitID"])).First().UName + "*";
                                    if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[2]["Number"].ToString()))
                                    {
                                        guige += dt_RawMaterial_Unit.Rows[2]["Number"].ToString();
                                        guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[2]["UnitID"])).First().UName + "*";
                                        if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[3]["Number"].ToString()))
                                        {
                                            guige += dt_RawMaterial_Unit.Rows[3]["Number"].ToString();
                                            guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[2]["UnitID"])).First().UName;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {

                    }
                    dr["Numbers"] = guige;
                    #endregion
                    dt.Rows.Add(dr);
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    decimal fromRe = 0;
                    for (int ii = 0; ii < dttempChuKu.Rows.Count; ii++)
                    {
                        var dtId = dt.Rows[i]["RawMaterialid"].ToString();
                        var chukuid = dttempChuKu.Rows[ii]["RAWID"].ToString();
                        if (dtId.Equals(chukuid))
                        {
                            dt.Rows[i]["Januaryc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["1"]) / 1000, 2, 3);
                            dt.Rows[i]["Februaryc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["2"]) / 1000, 2, 3);
                            dt.Rows[i]["Marchc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["3"]) / 1000, 2, 3);
                            dt.Rows[i]["Aprilc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["4"]) / 1000, 2, 3);
                            dt.Rows[i]["Mayc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["5"]) / 1000, 2, 3);
                            dt.Rows[i]["Junec"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["6"]) / 1000, 2, 3);
                            dt.Rows[i]["Julyc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["7"]) / 1000, 2, 3);
                            dt.Rows[i]["Augustc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["8"]) / 1000, 2, 3);
                            dt.Rows[i]["Septemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["9"]) / 1000, 2, 3);
                            dt.Rows[i]["Octoberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["10"]) / 1000, 2, 3);
                            dt.Rows[i]["Novemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["11"]) / 1000, 2, 3);
                            dt.Rows[i]["Decemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii]["12"]) / 1000, 2, 3);

                            for (int iii = 0; iii < 12; iii++)
                            {
                                fromRe += Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii][(iii + 1).ToString()]) / 1000, 2, 3);
                            }
                            decimal indb = Toolss.GetMathResult(FormatDecimal(dt.Rows[i]["id"]), 2, 3);
                            decimal scpd = Toolss.GetMathResult(FormatDecimal(dt.Rows[i]["temp1"]), 2, 3);
                            dt.Rows[i]["IntDate"] = fromRe;
                            dt.Rows[i]["RMID"] = (indb - fromRe) + scpd;
                            dttempChuKu.Rows.Remove(dttempChuKu.Rows[ii]);
                            break;
                        }
                    }
                }
                if (dttempChuKu.Rows.Count > 0)
                {
                    for (int i = 0; i < dttempChuKu.Rows.Count; i++)
                    {
                        decimal fromRe = 0;
                        DataRow dr = dt.NewRow();
                        dr["RawId"] = dttempChuKu.Rows[i]["RTID"];
                        dr["TypeName"] = dttempChuKu.Rows[i]["RTName"];
                        dr["RawName"] = dttempChuKu.Rows[i]["RawName"];
                        dr["Name"] = dttempChuKu.Rows[i]["RInfoName"];
                        dr["RawMaterialid"] = dttempChuKu.Rows[i]["RAWID"];
                        dr["Januaryc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["1"]) / 1000, 2, 3);
                        dr["Februaryc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["2"]) / 1000, 2, 3);
                        dr["Marchc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["3"]) / 1000, 2, 3);
                        dr["Aprilc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["4"]) / 1000, 2, 3);
                        dr["Mayc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["5"]) / 1000, 2, 3);
                        dr["Junec"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["6"]) / 1000, 2, 3);
                        dr["Julyc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["7"]) / 1000, 2, 3);
                        dr["Augustc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["8"]) / 1000, 2, 3);
                        dr["Septemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["9"]) / 1000, 2, 3);
                        dr["Octoberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["10"]) / 1000, 2, 3);
                        dr["Novemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["11"]) / 1000, 2, 3);
                        dr["Decemberc"] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i]["12"]) / 1000, 2, 3);
                        for (int iii = 0; iii < 12; iii++)
                        {
                            fromRe += Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i][(iii + 1).ToString()]) / 1000, 2, 3);
                        }
                        dr["IntDate"] = fromRe;
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 消耗计算 月条件
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="year"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetXiaohao_CalForMonth(string pid, string AreaID, string ClassID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.View_XiaohaoCalForMonthDataTable dt = new CMS_CateringDataSet.View_XiaohaoCalForMonthDataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttempRuKu = dvf.GetXiaohao_CalForMonth(strarealist, AreaID, ClassID, DTIME);//4.获取当前出库数据
            DataTable dttempChuKu = dvf.GetViewFromRecordsXiaoHaoCalByMonth(strarealist, AreaID, ClassID, DTIME);
            List<Catering_Model.Units> listUnits = new Catering_Rules.Units().GetModelList("");
            List<Catering_Model.RawMaterialsInfo> listRawInfo = new Catering_Rules.RawMaterialsInfo().GetModelList("");
            string tName = "";
            if (!string.IsNullOrEmpty(ClassID) && !ClassID.Equals("-10"))
            {
                tName = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(ClassID)).CName;
            }
            else if (!string.IsNullOrEmpty(AreaID) && !AreaID.Equals("-10"))
            {
                tName = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(AreaID)).AreaName;
            }
            if (dttempChuKu != null && dttempChuKu.Rows.Count > 0 && dttempRuKu != null && dttempRuKu.Rows.Count > 0)
            {
                for (int i = 0; i < dttempRuKu.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Number"] = Title;
                    if (!string.IsNullOrEmpty(tName))
                    {
                        dr["mymonth"] = tName;
                    }
                    decimal indb = 0;
                    dr["RawId"] = dttempRuKu.Rows[i]["RTID"];
                    dr["TypeName"] = dttempRuKu.Rows[i]["RTName"];
                    dr["RawName"] = dttempRuKu.Rows[i]["RawName"];
                    dr["Name"] = dttempRuKu.Rows[i]["RInfoName"];
                    dr["RawMaterialid"] = dttempRuKu.Rows[i]["RAWID"];
                    for (int ii = 1; ii <= 31; ii++)
                    {
                        var tempNum = Toolss.GetMathResult(FormatDecimal(dttempRuKu.Rows[i]["R" + ii]) / 1000, 2, 3);
                        if (tempNum > 0)
                        {
                            dr["R" + ii] = tempNum;
                        }
                        indb += FormatDecimal(dttempRuKu.Rows[i]["R" + ii]);
                    }
                    Catering_Model.E_RawMaterial model = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(dr["RawMaterialid"]));
                    string balace = dvf.GetInventory(model.id.ToString(), DTIME.Year, DTIME.Month, ClassID); //库存盘点                    
                    if (!string.IsNullOrEmpty(balace))
                    {
                        bool iskg = true;
                        decimal guige01 = 1;
                        //入库单位
                        string danwei = new Catering_Rules.Units().GetModel(Convert.ToInt32(model.temp2)).UName;
                        decimal UtilKg = Toolss.GetSpecifications(Convert.ToInt32(dr["RawMaterialid"]), danwei, out iskg);
                        guige01 = iskg ? 1000 : 1;
                        decimal weight = Toolss.GetMathResult(UtilKg * Convert.ToDecimal(balace), 2, 3);
                        dr["temp1"] = Toolss.GetMathResult(weight * guige01 / 1000, 2, 3);
                    }
                    else
                    {
                        dr["temp1"] = 0;
                    }
                    indb = Toolss.GetMathResult(GetInDBResult(dttempRuKu.Rows[i]) / 1000, 2, 3);
                    dr["id"] = indb;
                    #region 绑定单位
                    string guige = "";
                    try
                    {
                        DataTable dt_RawMaterial_Unit = new Catering_Rules.RawMaterial_Unit().GetList("RawMaterialID=" + dttempRuKu.Rows[i]["RAWID"].ToString()).Tables[0];

                        if (dt_RawMaterial_Unit != null && dt_RawMaterial_Unit.Rows.Count > 0)
                        {
                            if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[0]["Number"].ToString()))
                            {
                                guige += dt_RawMaterial_Unit.Rows[0]["Number"].ToString();
                                guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[0]["UnitID"])).First().UName + "*";
                                if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[1]["Number"].ToString()))
                                {
                                    guige += dt_RawMaterial_Unit.Rows[1]["Number"].ToString();
                                    guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[1]["UnitID"])).First().UName + "*";
                                    if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[2]["Number"].ToString()))
                                    {
                                        guige += dt_RawMaterial_Unit.Rows[2]["Number"].ToString();
                                        guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[2]["UnitID"])).First().UName + "*";
                                        if (!string.IsNullOrEmpty(dt_RawMaterial_Unit.Rows[3]["Number"].ToString()))
                                        {
                                            guige += dt_RawMaterial_Unit.Rows[3]["Number"].ToString();
                                            guige += listUnits.Where(p => p.id == Convert.ToInt32(dt_RawMaterial_Unit.Rows[2]["UnitID"])).First().UName;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {

                    }
                    dr["Numbers"] = guige;
                    #endregion
                    dt.Rows.Add(dr);
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                decimal fromRe = 0;
                for (int ii = 0; ii < dttempChuKu.Rows.Count; ii++)
                {
                    var dtId = dt.Rows[i]["RawMaterialid"].ToString();
                    var chukuid = dttempChuKu.Rows[ii]["RAWID"].ToString();
                    if (dtId.Equals(chukuid))
                    {
                        for (int iii = 1; iii <= 31; iii++)
                        {
                            if (FormatDecimal(dttempChuKu.Rows[ii][iii.ToString()]) > 0)
                            {
                                dt.Rows[i]["C" + iii] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[ii][iii.ToString()]) / 1000, 2, 3);
                            }
                            fromRe += FormatDecimal(dttempChuKu.Rows[ii][iii.ToString()]);
                        }
                        fromRe = Toolss.GetMathResult(fromRe / 1000, 2, 3);
                        decimal indb = Toolss.GetMathResult(FormatDecimal(dt.Rows[i]["id"]), 2, 3);
                        decimal scpd = FormatDecimal(dt.Rows[i]["temp1"]);
                        dt.Rows[i]["IntDate"] = fromRe;
                        dt.Rows[i]["RMID"] = (indb - fromRe) + scpd;
                        dttempChuKu.Rows.Remove(dttempChuKu.Rows[ii]);
                        break;
                    }
                }
            }
            if (dttempChuKu.Rows.Count > 0)
            {
                for (int i = 0; i < dttempChuKu.Rows.Count; i++)
                {
                    decimal fromRe = 0;
                    DataRow dr = dt.NewRow();
                    dr["RawId"] = dttempChuKu.Rows[i]["RTID"];
                    dr["TypeName"] = dttempChuKu.Rows[i]["RTName"];
                    dr["RawName"] = dttempChuKu.Rows[i]["RawName"];
                    dr["Name"] = dttempChuKu.Rows[i]["RInfoName"];
                    dr["RawMaterialid"] = dttempChuKu.Rows[i]["RAWID"];
                    for (int ii = 1; ii <= 31; ii++)
                    {
                        if (FormatDecimal(dttempChuKu.Rows[i][ii.ToString()]) > 0)
                        {
                            dr["C" + ii] = Toolss.GetMathResult(FormatDecimal(dttempChuKu.Rows[i][ii.ToString()]) / 1000, 2, 3);
                        }
                        fromRe += FormatDecimal(dttempChuKu.Rows[i][ii.ToString()]);
                    }
                    fromRe = Toolss.GetMathResult(fromRe / 1000, 2, 3);
                    dr["IntDate"] = fromRe;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 入库合计
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private decimal GetInDBResult(DataRow dr)
        {
            decimal count = 0;
            if (dr.Table.Columns.Count <= 28)
            {
                object[] obj = new object[]
            {
                    dr["1r"],
                    dr["2r"],
                    dr["3r"],
                    dr["4r"],
                    dr["5r"], 
                    dr["6r"],
                    dr["7r"],
                    dr["8r"],
                    dr["9r"],
                    dr["10r"],
                    dr["11r"],
                    dr["12r"]
            };
                for (int i = 0; i < obj.Count(); i++)
                {
                    count += Toolss.GetMathResult(FormatDecimal(obj[i]) / 1000, 2, 3);
                }
            }
            else
            {
                for (int i = 1; i <= 31; i++)
                {
                    count += FormatDecimal(dr["R" + i]);
                }
            }
            return count;
        }
        /// <summary>
        /// 出库合计
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private decimal GetFormResult(DataRow dr)
        {
            decimal count = 0;
            if (dr.Table.Columns.Count == 28)
            {
                object[] obj = new object[] 
            {
                dr["1c"],
                dr["2c"],
                dr["3c"],
                dr["4c"],
                dr["5c"],
                dr["6c"],
                dr["7c"],
                dr["8c"],
                dr["9c"],
                dr["10c"],
                dr["11c"],
                dr["12c"]
            };
                for (int i = 0; i < obj.Count(); i++)
                {
                    count += FormatDecimal(obj[i]);
                }
            }
            else
            {
                for (int i = 1; i <= 31; i++)
                {
                    count += FormatDecimal(dr["C" + i]);
                }
            }

            return count;
        }
        #endregion

        #region 出库动态
        /// <summary>
        /// 出库动态
        /// </summary>
        /// <returns></returns>
        public DataView GetMonitorList(DateTime dTime)
        {
            //try
            //{
            List<Catering_DataSet.monitorViewRow> model = new List<Catering_DataSet.monitorViewRow>();
            Catering_Rules.ClassInfo classinfo = new Catering_Rules.ClassInfo();
            DataSet dsc = classinfo.GetAllList();
            List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetModelList(" AreaID <> 6").OrderBy(p => p.AreaID).ToList<Catering_Model.ClassInfo>();
            //for (int i = 0; i < dsc.Tables[0].Rows.Count; i++)
            //{
            //    Catering_Model.ClassInfo cinfo = new Catering_Model.ClassInfo();
            //    cinfo.id = Int32.Parse(dsc.Tables[0].Rows[i]["id"].ToString());
            //    cinfo.CName = dsc.Tables[0].Rows[i]["CName"].ToString();
            //    cinfo.AreaID = Int32.Parse(dsc.Tables[0].Rows[i]["AreaID"].ToString());
            //    if (cinfo.AreaID == 6) continue;
            //    listClass.Add(cinfo);
            //}

            Catering_Rules.OperationArea oArea = new Catering_Rules.OperationArea();
            DataSet dso = oArea.GetAllList();
            List<Catering_Model.OperationArea> listArea = new Catering_Rules.OperationArea().GetModelList("").OrderBy(p => p.id).ToList<Catering_Model.OperationArea>();
            //for (int i = 0; i < dso.Tables[0].Rows.Count; i++)
            //{
            //    Catering_Model.OperationArea oAreaInfo = new Catering_Model.OperationArea();
            //    oAreaInfo.id = Int32.Parse(dso.Tables[0].Rows[i]["id"].ToString());
            //    oAreaInfo.AreaName = dso.Tables[0].Rows[i]["AreaName"].ToString();
            //    listArea.Add(oAreaInfo);
            //}

            Catering_Rules.Monitor monitorRules = new Catering_Rules.Monitor();
            DataSet dsm = monitorRules.GetAllList(dTime);
            List<Catering_Model.Monitor> listMonitor = new List<Catering_Model.Monitor>();
            for (int i = 0; i < dsm.Tables[0].Rows.Count; i++)
            {
                Catering_Model.Monitor monitorInfo = new Catering_Model.Monitor();
                monitorInfo.id = Int32.Parse(dsm.Tables[0].Rows[i]["id"].ToString());
                monitorInfo.ClassID = Int32.Parse(dsm.Tables[0].Rows[i]["ClassID"].ToString());
                monitorInfo.GroupID = Int32.Parse(dsm.Tables[0].Rows[i]["GroupID"].ToString());
                monitorInfo.OutDate = dsm.Tables[0].Rows[i]["OutDate"].ToString();
                monitorInfo.OutState = dsm.Tables[0].Rows[i]["OutState"].ToString();
                listMonitor.Add(monitorInfo);
            }
            DataTable dt = new Catering_DataSet.monitorViewDataTable();
            for (int i = 0; i < listClass.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ClassId"] = listClass[i].id;
                dr["ClassName"] = listClass[i].CName;
                dr["GroupId"] = listArea.Where(p => p.id == listClass[i].AreaID).First().id;
                dr["GroupName"] = listArea.Where(p => p.id == listClass[i].AreaID).First().AreaName;
                int Groupid;
                Int32.TryParse(dr["GroupId"].ToString(), out Groupid);
                int Classid;
                Int32.TryParse(dr["ClassId"].ToString(), out Classid);
                Catering_Model.Monitor monInfo;
                if (listMonitor.Where(m => m.GroupID == Groupid && m.ClassID == Classid).Count() > 0)
                {
                    monInfo = listMonitor.Where(m => m.GroupID == Groupid && m.ClassID == Classid).First();
                    if (monInfo != null && !string.IsNullOrEmpty(monInfo.OutState))
                    {
                        dr["Id"] = monInfo.id;
                        dr["OutDate"] = monInfo.OutDate;
                        dr["OutState"] = monInfo.OutState;
                    }
                }
                dt.Rows.Add(dr);
            }
            DataView dv = dt.DefaultView;
            dv.Table.Columns.Add(new DataColumn("tempid"));
            dv.Sort = "OutDate desc";
            for (int i = 0; i < dv.Table.Rows.Count; i++)
            {
                dv.Table.Rows[i]["tempid"] = i + 1;
            }
            return dv;
            //}
            //catch ( Exception ex)
            //{
            //    return null;
            //}
        }
        #endregion

        #region 人均累计百分比
        /// <summary>
        /// 年条件
        /// </summary>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="YEAR"></param>
        /// <param name="TITLE"></param>
        /// <returns></returns>
        public DataTable GetTotalPercent(string pid, string AREAID, string CLASSID, string YEAR, string TITLE)
        {
            CMS_CateringDataSet.View_TotalPercentDataTable dt = new CMS_CateringDataSet.View_TotalPercentDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetTotalPercent(strclasslist, AREAID, CLASSID, YEAR);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            listRaws = listRaws.Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6).ToList<Catering_Model.E_RawMaterials>();
            foreach (Catering_Model.E_RawMaterials itemRaws in listRaws)
            {
                DataRow dr = dt.NewRow();
                dr["TypeName"] = itemRaws.TypeName;
                dr["Title"] = TITLE;
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    if (itemRaws.id == Convert.ToInt32(dttemp.Rows[i]["id"]))
                    {
                        dr["Numbers"] = dttemp.Rows[i]["Numbers"];
                        break;
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 年条件
        /// </summary>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="YEAR"></param>
        /// <param name="TITLE"></param>
        /// <returns></returns>
        public DataTable GetTotalPercentMonth(string pid, string AREAID, string CLASSID, string YEAR, string TITLE)
        {
            CMS_CateringDataSet.View_TotalPercentDataTable dt = new CMS_CateringDataSet.View_TotalPercentDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetTotalPercentMonth(strclasslist, AREAID, CLASSID, YEAR);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            listRaws = listRaws.Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6).ToList<Catering_Model.E_RawMaterials>();
            foreach (Catering_Model.E_RawMaterials itemRaws in listRaws)
            {
                for (int i = 1; i <= 12; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["TypeName"] = itemRaws.TypeName;
                    dr["Month"] = i;
                    dr["Title"] = TITLE;
                    for (int ii = 0; ii < dttemp.Rows.Count; ii++)
                    {
                        if (itemRaws.id == Convert.ToInt32(dttemp.Rows[ii]["id"]) && Convert.ToInt32(dttemp.Rows[ii]["month"]) == i)
                        {
                            dr["Numbers"] = dttemp.Rows[ii]["Numbers"];
                            break;
                        }
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }


        /// <summary>
        /// 月条件
        /// </summary>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="YEAR"></param>
        /// <param name="TITLE"></param>
        /// <returns></returns>
        public DataTable GetTotalPercentForMonth(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.View_TotalPercentDataTable dt = new CMS_CateringDataSet.View_TotalPercentDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetTotalPercentForMonth(strclasslist, AREAID, CLASSID, DTIME);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            listRaws = listRaws.Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6).ToList<Catering_Model.E_RawMaterials>();
            foreach (Catering_Model.E_RawMaterials itemRaws in listRaws)
            {
                DataRow dr = dt.NewRow();
                dr["TypeName"] = itemRaws.TypeName;
                dr["Title"] = TITLE;
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    if (itemRaws.id == Convert.ToInt32(dttemp.Rows[i]["id"]))
                    {
                        dr["Numbers"] = dttemp.Rows[i]["Numbers"];
                        break;
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 月条件
        /// </summary>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="YEAR"></param>
        /// <param name="TITLE"></param>
        /// <returns></returns>
        public DataTable GetTotalPercentForDay(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.View_TotalPercentDataTable dt = new CMS_CateringDataSet.View_TotalPercentDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetTotalPercentForDay(strclasslist, AREAID, CLASSID, DTIME);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            listRaws = listRaws.Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6).ToList<Catering_Model.E_RawMaterials>();
            foreach (Catering_Model.E_RawMaterials itemRaws in listRaws)
            {
                for (int i = 1; i <= 31; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["TypeName"] = itemRaws.TypeName;
                    dr["Month"] = i;
                    dr["Title"] = TITLE;
                    for (int ii = 0; ii < dttemp.Rows.Count; ii++)
                    {
                        if (itemRaws.id == Convert.ToInt32(dttemp.Rows[ii]["id"]) && Convert.ToInt32(dttemp.Rows[ii]["day"]) == i)
                        {
                            dr["Numbers"] = dttemp.Rows[ii]["Numbers"];
                            break;
                        }
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }


        /// <summary>
        /// 标准比列参照图
        /// </summary>
        /// <returns></returns>
        public DataTable GetTotalPercentStandard()
        {
            CMS_CateringDataSet.View_TotalPercentDataTable dt = new CMS_CateringDataSet.View_TotalPercentDataTable();
            DataRow dr1 = dt.NewRow(); dr1["TypeName"] = "粮食类"; dr1["Numbers"] = 27;
            DataRow dr2 = dt.NewRow(); dr2["TypeName"] = "肉类"; dr2["Numbers"] = 5;
            DataRow dr3 = dt.NewRow(); dr3["TypeName"] = "海鲜类"; dr3["Numbers"] = 7;
            DataRow dr4 = dt.NewRow(); dr4["TypeName"] = "蔬菜类"; dr4["Numbers"] = 34;
            DataRow dr5 = dt.NewRow(); dr5["TypeName"] = "水果类"; dr5["Numbers"] = 27;
            dt.Rows.Add(dr2); dt.Rows.Add(dr3); dt.Rows.Add(dr4); dt.Rows.Add(dr5); dt.Rows.Add(dr1);
            return dt;
        }
        #endregion

        #region 大类转换种属
        private string EXChangeRAW(string RAWNAME)
        {
            switch (RAWNAME)
            {
                case "肉类":
                    RAWNAME = "肉类";
                    break;
                case "水产类":
                    RAWNAME = "鱼虾类";
                    break;
                case "蔬菜类":
                    RAWNAME = "蔬菜类";
                    break;
                case "水果类":
                    RAWNAME = "水果类";
                    break;
                case "粮食类":
                    RAWNAME = "谷类薯类及杂豆";
                    break;
            }
            return RAWNAME;
        }
        #endregion

        #region 分类统计
        /// <summary>
        /// 分类统计
        /// </summary>
        /// <returns></returns>
        public DataTable GetXiaohaoChar(string pid, string groupId, string[] dTime, int[] rawId, string classId)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Numbers", typeof(decimal));//增加字段Numbers
                dt.Columns.Add("FoodType", typeof(string));//增加字段name
                dt.Columns.Add("Month", typeof(int));//增加字段Month
                dt.Columns.Add("NumYear", typeof(int));//增加字段NumYear
                dt.Columns.Add("OutDate", typeof(string));//增加字段OutDate
                string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                DataTable dtChar = new DataTable();
                if (dTime != null && dTime.Count() > 0)
                {
                    for (int di = 0; di < dTime.Count(); di++)
                    {
                        if (Convert.ToInt32(dTime[di]) < 2012)
                        {
                            dtChar = dvf.GetTypeCharDBHistoryData(strarealist, groupId, dTime, rawId, classId);
                            if (dtChar != null && dtChar.Rows.Count >= 1)
                            {
                                for (int i = 0; i < dtChar.Rows.Count; i++)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["NumYear"] = Convert.ToInt32(dtChar.Rows[i]["NumYear"]);

                                    dr["FoodType"] = EXChangeRAW(dtChar.Rows[i]["TypeName"].ToString());

                                    decimal numbers = 0;
                                    decimal.TryParse(dtChar.Rows[i]["SumNumber"].ToString(), out numbers);
                                    dr["Numbers"] = numbers;
                                    dr["Month"] = Convert.ToInt32(dtChar.Rows[i]["month"]);
                                    dr["OutDate"] = dtChar.Rows[i]["OutDate"].ToString();
                                    dt.Rows.Add(dr);
                                }
                            }
                            break;
                        }
                    }
                }


                dtChar = dvf.GetTypeCharDB(strarealist, groupId, dTime, rawId, classId);
                if (dtChar != null && dtChar.Rows.Count >= 1)
                {
                    for (int i = 0; i < dtChar.Rows.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["NumYear"] = Convert.ToInt32(dtChar.Rows[i]["NumYear"]);
                        dr["FoodType"] = dtChar.Rows[i]["TypeName"].ToString();
                        decimal numbers = 0;
                        decimal.TryParse(dtChar.Rows[i]["SumNumber"].ToString(), out numbers);
                        dr["Numbers"] = numbers;
                        dr["Month"] = Convert.ToInt32(dtChar.Rows[i]["month"]);
                        dr["OutDate"] = dtChar.Rows[i]["OutDate"].ToString();
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 分类统计
        /// </summary>
        /// <returns></returns>
        public DataTable GetXiaohaoCharByMonth(string pid, string groupId, DateTime dTime, int[] rawId, string classId)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Numbers", typeof(decimal));//增加字段Numbers
                dt.Columns.Add("FoodType", typeof(string));//增加字段name
                dt.Columns.Add("Day", typeof(int));//增加字段Month
                dt.Columns.Add("OutDate", typeof(string));//增加字段OutDate
                string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                DataTable dtChar = dvf.GetTypeCharDBByMonth(strarealist, groupId, dTime, rawId, classId);
                if (dtChar != null && dtChar.Rows.Count >= 1)
                {
                    for (int i = 0; i < dtChar.Rows.Count; i++)
                    {

                        DataRow dr = dt.NewRow();
                        dr["Day"] = dtChar.Rows[i]["Day"];
                        dr["FoodType"] = dtChar.Rows[i]["TypeName"].ToString();
                        decimal numbers = 0;
                        decimal.TryParse(dtChar.Rows[i]["SumNumber"].ToString(), out numbers);
                        dr["Numbers"] = numbers;
                        dr["OutDate"] = dtChar.Rows[i]["OutDate"].ToString();
                        dt.Rows.Add(dr);

                    }
                    //}
                }
                return dt;
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region 分类对比
        /// <summary>
        /// 分类统计
        /// </summary>
        /// <returns></returns>
        public DataTable GetXiaohao_Contrast(string pid, string groupId, string classid, string[] dTime, int[] rawId)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            return dvf.GetXiaohao_Contrast(strarealist, groupId, classid, dTime, rawId);
        }
        public DataTable GetPerson_Avg_ContrastHistoryByYear(string pid, string groupId, string classid, string[] dTime, int[] rawId)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            return dvf.GetPerson_Avg_ContrastHistoryByYear(strarealist, dTime, groupId, classid, rawId);
        }

        public DataTable GetXiaohao_ContrastByMonth(string pid, string areaId, string classID, DateTime DTIME, int[] RawId)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            return dvf.GetXiaohao_ContrastForMonth(strarealist, areaId, classID, DTIME, RawId);
        }

        public DataTable GetXiaohao_ContrastByDay(string pid, string areaId, string classID, DateTime DTIME, int[] RawId)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            return dvf.GetXiaohao_ContrastForDay(strarealist, areaId, classID, DTIME, RawId);
        }

        #endregion

        #region 食用油统计
        public DataTable GetOilResult(string CLASSID, string YEAR)
        {
            CMS_CateringDataSet.View_EdibleStatisticsRPDataTable dt = new CMS_CateringDataSet.View_EdibleStatisticsRPDataTable();
            #region 获取数据
            DataTable dtDy = dvf.GetAteNumber(CLASSID, YEAR);//获取12个月就餐总数
            var dynamicList = from d in dtDy.AsEnumerable()
                              select new
                              {
                                  Year = Convert.ToInt32(d["yearofdate"]),
                                  Month = Convert.ToInt32(d["monthofdate"]),
                                  Eat = Convert.ToInt32(d["Eat"])
                              };
            DataTable dtPurchaseInfo = dvf.GetView_PurchaseInfo("20", CLASSID, YEAR);//获取12个月采购数据
            //PNumber计划数量,RNumber需采购量
            var purchaseList = from p in dtPurchaseInfo.AsEnumerable()
                               select new
                               {
                                   RawID = Convert.ToInt32(p["DID"]),
                                   PNumber = Convert.ToInt32(p["ProgramNumber"]),
                                   RNumber = Convert.ToInt32(p["Requirement"]),
                                   Month = Convert.ToInt32(p["Date"])
                               };
            DataTable dtFromRecords = dvf.GetRecords(CLASSID, YEAR, "");
            var fromRecordList = from f in dtFromRecords.AsEnumerable()
                                 select new
                                 {
                                     Numbers = Convert.ToInt32(f["Numbers"]),
                                     Month = Convert.ToInt32(f["OutDate"])
                                 };
            #endregion
            Dictionary<int, DataRow> dichafYear = new Dictionary<int, DataRow>();
            decimal MN = 0, CN = 0, NN = 0, EN = 0, AN = 0, PA = 0;
            for (int i = 1; i <= 12; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Month"] = i;
                DataRow dr2 = dt.NewRow();
                if (i == 6)
                {
                    dr2["Month"] = "上半年小计";
                }
                else if (i == 12)
                {
                    dr2["Month"] = "下半年小计";
                }
                if (purchaseList.Where(p => p.Month == i).Count() > 0 && dynamicList.Where(p => p.Month == i).Count() > 0 && fromRecordList.Where(p => p.Month == i).Count() > 0)
                {
                    var pTemp = purchaseList.Where(p => p.Month == i).First();
                    var dTemp = dynamicList.Where(p => p.Month == i).First();
                    var fTemp = fromRecordList.Where(p => p.Month == i).First();
                    MN += pTemp.RNumber;
                    CN += fTemp.Numbers;
                    NN += Convert.ToDecimal(dTemp.Eat * 0.03);
                    EN += Convert.ToDecimal((fTemp.Numbers - dTemp.Eat) * 0.03);
                    AN += dTemp.Eat;
                    PA += Convert.ToDecimal(fTemp.Numbers / dTemp.Eat);
                    if (i == 6 || i == 12)
                    {
                        dr2["MaterialNumber"] = MN;
                        dr2["ConsumeNumber"] = CN;
                        dr2["NormalNumber"] = NN;
                        dr2["ExceedNumber"] = EN;
                        dr2["AteNumber"] = AN;
                        dr2["PersonAvg"] = PA;
                        MN = 0; CN = 0; NN = 0; EN = 0; AN = 0; PA = 0;

                    }
                    dr["MaterialNumber"] = pTemp.RNumber;
                    dr["ConsumeNumber"] = fTemp.Numbers;
                    dr["NormalNumber"] = dTemp.Eat * 0.03;
                    dr["ExceedNumber"] = ((fTemp.Numbers - dTemp.Eat) * 0.03);
                    dr["AteNumber"] = dTemp.Eat;
                    dr["PersonAvg"] = fTemp.Numbers / dTemp.Eat;
                }
                if (i == 6 || i == 12)
                {
                    dichafYear.Add(i, dr2);
                }
                dt.Rows.Add(dr);
            }
            if (dichafYear.Count == 2)
            {
                dt.Rows.Add(dichafYear[6].ItemArray); dt.Rows.Add(dichafYear[12].ItemArray);
            }

            return dt;
        }

        public DataTable GetOilResultByMonth(int companyid, string AREAID, string CLASSID, string YEAR, string MONTH)
        {
            CMS_CateringDataSet.OilOfYearDataTable dt = new CMS_CateringDataSet.OilOfYearDataTable();
            #region 获取数据
            DataTable dtDy = dvf.GetAteNumber(CLASSID, YEAR, MONTH);//获取具体月份就餐总数
            var dynamicList = from d in dtDy.AsEnumerable()
                              select new
                              {
                                  Year = Convert.ToInt32(d["yearofdate"]),
                                  Month = Convert.ToInt32(d["monthofdate"]),
                                  Eat = Convert.ToInt32(d["Eat"]),
                                  ClassID = Convert.ToInt32(d["ClassID"])
                              };
            DataTable dtPurchaseInfo = dvf.GetView_PurchaseInfo("20", CLASSID, YEAR, MONTH);//获取具体月份采购数据
            //PNumber计划数量,RNumber需采购量
            var purchaseList = from p in dtPurchaseInfo.AsEnumerable()
                               select new
                               {
                                   RawID = Convert.ToInt32(p["DID"]),
                                   PNumber = Convert.ToInt32(p["ProgramNumber"]),
                                   RNumber = Convert.ToInt32(p["Requirement"]),
                                   Month = Convert.ToInt32(p["Date"]),
                                   CName = p["CName"],
                                   ClassID = Convert.ToInt32(p["ClassID"])
                               };
            DataTable dtFromRecords = dvf.GetRecords(CLASSID, YEAR, MONTH);
            var fromRecordList = from f in dtFromRecords.AsEnumerable()
                                 select new
                                 {
                                     Numbers = Convert.ToInt32(f["Numbers"]),
                                     Month = Convert.ToInt32(f["OutDate"]),
                                     ClassID = Convert.ToInt32(f["ClassID"])
                                 };
            #endregion
            Catering_Rules.OperationArea OAreaRules = new Catering_Rules.OperationArea();
            DataTable dtOArea = null;
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
            {
                dtOArea = OAreaRules.GetList(string.Format(" id = {0}", AREAID)).Tables[0];
            }
            else
            {
                dtOArea = OAreaRules.GetAllListByCompanyID(companyid).Tables[0];
            }
            Catering_Rules.ClassInfo ClassRules = new Catering_Rules.ClassInfo();
            DataTable dtClass = classRules.GetAllList().Tables[0];
            var classTemp = from c in dtClass.AsEnumerable()
                            select new
                            {
                                id = Convert.ToInt32(c["id"]),
                                cName = c["CName"].ToString(),
                                areaID = Convert.ToInt32(c["AreaID"])
                            };
            for (int i = 0; i < dtOArea.Rows.Count; i++)
            {
                foreach (var ClassItem in classTemp.Where(p => p.areaID == Convert.ToInt32(dtOArea.Rows[i]["id"])))
                {
                    DataRow dr1 = dt.NewRow();
                    DataRow dr2 = dt.NewRow();
                    DataRow dr3 = dt.NewRow();
                    int mon = Convert.ToInt32(MONTH);
                    dr1["ClassName"] = ClassItem.cName;
                    dr2["ClassName"] = ClassItem.cName;
                    dr3["ClassName"] = ClassItem.cName;
                    dr1["Name"] = "要料量";
                    dr2["Name"] = "标准值";
                    dr3["Name"] = "实际消耗量";
                    if (purchaseList.Where(p => p.Month == mon && ClassItem.id == p.ClassID).Count() > 0 && dynamicList.Where(p => p.Month == mon && p.ClassID == ClassItem.id).Count() > 0 && fromRecordList.Where(p => p.Month == mon && p.ClassID == ClassItem.id).Count() > 0)
                    {
                        var pTemp = purchaseList.Where(p => p.Month == mon && p.ClassID == ClassItem.id).First();
                        var dTemp = dynamicList.Where(p => p.Month == mon && p.ClassID == ClassItem.id).First();
                        var fTemp = fromRecordList.Where(p => p.Month == mon && p.ClassID == ClassItem.id).First();

                        dr1["Value"] = pTemp.RNumber;//要料量
                        dr2["Value"] = fTemp.Numbers;//消耗量
                        dr3["Value"] = dTemp.Eat * 0.03;//标准值
                    }
                    dt.Rows.Add(dr1);
                    dt.Rows.Add(dr2);
                    dt.Rows.Add(dr3);
                }
            }
            return dt;
        }

        public DataTable GetOilOfYearResult(string CLASSID, string YEAR)
        {
            CMS_CateringDataSet.OilOfYearDataTable dt = new CMS_CateringDataSet.OilOfYearDataTable();
            #region 获取数据
            DataTable dtDy = dvf.GetAteNumberHallYear(CLASSID, YEAR);//获取整年就餐总数
            var dynamicList = from d in dtDy.AsEnumerable()
                              select new
                              {
                                  Year = Convert.ToInt32(d["yearofdate"]),
                                  Eat = Convert.ToInt32(d["Eat"])
                              };
            DataTable dtPurchaseInfo = dvf.GetView_PurchaseInfoForYear("20", CLASSID, YEAR);//获取12个月采购数据
            //PNumber计划数量,RNumber需采购量
            var purchaseList = from p in dtPurchaseInfo.AsEnumerable()
                               select new
                               {
                                   RawID = Convert.ToInt32(p["DID"]),
                                   PNumber = Convert.ToInt32(p["ProgramNumber"]),
                                   RNumber = Convert.ToInt32(p["Requirement"])
                               };
            DataTable dtFromRecords = dvf.GetRecordsHallYear(CLASSID, YEAR);
            var fromRecordList = from f in dtFromRecords.AsEnumerable()
                                 select new
                                 {
                                     Numbers = Convert.ToInt32(f["Numbers"]),
                                     Year = Convert.ToInt32(f["OutDate"])
                                 };
            #endregion
            DataRow dr1 = dt.NewRow();
            DataRow dr2 = dt.NewRow();
            DataRow dr3 = dt.NewRow();
            dr1["Name"] = "食用油标准消耗值";
            dr2["Name"] = "食用油消耗数量";
            dr3["Name"] = "食用油要料数量";
            if (dynamicList.Count() == 1 && fromRecordList.Count() == 1)
            {
                dr1["value"] = dynamicList.First().Eat * 0.03;
                dr2["value"] = fromRecordList.First().Numbers;

            }
            if (purchaseList.Count() > 0)
            {
                decimal num = 0;
                foreach (var item in purchaseList)
                {
                    num += item.RNumber;
                }
                dr3["value"] = num;
            }
            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);
            return dt;
        }

        #endregion

        #region 年食用油
        public DataTable GetOilOfYearByDate(string year)
        {
            DataTable dt = new CMS_CateringDataSet.OilOfYearDataTable();
            DataViewForReport dvr = new DataViewForReport();
            DataTable temp = dvr.GetOilOfYearByDate(year);
            return dt;
        }
        #endregion

        #region 人均消耗分类汇总
        /// <summary>
        /// 人均消耗分类汇总
        /// </summary>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="DTIME"></param>
        /// <returns></returns>
        public DataTable GetPersonAvgXiaohaoType(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.View_XiaoHaoSumDataTable dt = new CMS_CateringDataSet.View_XiaoHaoSumDataTable();
            string CName = "所有班组";
            //DataTable dttemp = dvf.GetPersonAvgXiaohaoType(AREAID, CLASSID, DTIME);
            string Title2 = "";
            Title2 += "              单位：克";
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            if (!CLASSID.Equals("-10"))
            {
                CName = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(CLASSID)).CName;
            }
            else if (!AREAID.Equals("-10"))
            {
                CName = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(AREAID)).AreaName;
            }
            decimal[] NumOfPerson = new decimal[12];

            for (int i = 0; i < 12; i++)
            {
                decimal personNum = 0;
                DataTable dt_person = dvf.GetPersonNumbers(strarealist, AREAID, CLASSID, DTIME.Year, i + 1);
                if (dt_person != null && dt_person.Rows.Count > 0)
                {

                    try
                    {
                        personNum = Convert.ToDecimal(dt_person.Rows[0]["Number"]);
                    }
                    catch { }
                    dt_person.Dispose();
                }
                NumOfPerson[i] = personNum;
            }
            DataTable dttemp = dvf.GetPersonAvgXiaohaoType(strarealist, AREAID, CLASSID, DTIME);
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    int rawid = Convert.ToInt16(dttemp.Rows[i]["RAWZS"]);
                    dr["RawId"] = rawid;
                    dr["Price"] = TITLE;
                    dr["TypeName"] = dttemp.Rows[i]["RAWZSName"];
                    dr["CName"] = CName;
                    dr["Title"] = Title2;
                    dr["Name"] = dttemp.Rows[i]["Name"];

                    int tempCount = 0;
                    decimal tempNum1 = 0;

                    if (i == 0)
                    {
                        dr["E1"] = NumOfPerson[0];
                        dr["E2"] = NumOfPerson[1];
                        dr["E3"] = NumOfPerson[2];
                        dr["E4"] = NumOfPerson[3];
                        dr["E5"] = NumOfPerson[4];
                        dr["E6"] = NumOfPerson[5];
                        dr["E7"] = NumOfPerson[6];
                        dr["E8"] = NumOfPerson[7];
                        dr["E9"] = NumOfPerson[8];
                        dr["E10"] = NumOfPerson[9];
                        dr["E11"] = NumOfPerson[10];
                        dr["E12"] = NumOfPerson[11];
                    }

                    if (FormatDecimal(dttemp.Rows[i]["1"]) > 0 && NumOfPerson[0] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["1"]) / NumOfPerson[0], 0, 2);
                        dr["January"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["2"]) > 0 && NumOfPerson[1] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["2"]) / NumOfPerson[1], 0, 2);
                        dr["February"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["3"]) > 0 && NumOfPerson[2] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["3"]) / NumOfPerson[2], 0, 2);
                        dr["March"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }

                    if (FormatDecimal(dttemp.Rows[i]["4"]) > 0 && NumOfPerson[3] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["4"]) / NumOfPerson[3], 0, 2);
                        dr["April"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["5"]) > 0 && NumOfPerson[4] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["5"]) / NumOfPerson[4], 0, 2);
                        dr["May"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["6"]) > 0 && NumOfPerson[5] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["6"]) / NumOfPerson[5], 0, 2);
                        dr["June"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["7"]) > 0 && NumOfPerson[6] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["7"]) / NumOfPerson[6], 0, 2);
                        dr["July"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["8"]) > 0 && NumOfPerson[7] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["8"]) / NumOfPerson[7], 0, 2);
                        dr["August"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["9"]) > 0 && NumOfPerson[8] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["9"]) / NumOfPerson[8], 0, 2);
                        dr["September"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["10"]) > 0 && NumOfPerson[9] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["10"]) / NumOfPerson[9], 0, 2);
                        dr["October"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["11"]) > 0 && NumOfPerson[10] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["11"]) / NumOfPerson[10], 0, 2);
                        dr["November"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }
                    if (FormatDecimal(dttemp.Rows[i]["12"]) > 0 && NumOfPerson[11] > 0)
                    {
                        decimal tempnum = 0;
                        tempnum = GetMathResult(FormatDecimal(dttemp.Rows[i]["12"]) / NumOfPerson[11], 0, 2);
                        dr["December"] = tempnum;
                        if (tempnum > 0)
                        {
                            tempNum1 += tempnum;
                            tempCount++;
                        }
                    }

                    //for (int ii = 0; ii < 12; ii++)
                    //{
                    //    if (FormatDecimal(dttemp.Rows[i][(ii + 1).ToString()]) > 0 && NumOfPerson[ii] > 0)
                    //    {
                    //        tempCount = tempCount + 1;
                    //        tempNum = tempNum + (FormatDecimal(dttemp.Rows[i][(ii + 1).ToString()]) / NumOfPerson[ii]);
                    //    }
                    //}
                    if (tempCount > 0 && tempNum1 > 0)
                    {
                        dr["Inventory"] = GetMathResult(tempNum1 / tempCount, 0, 3);
                    }
                    //dr["Inventory"] = FormatDecimal(dr["January"]) + FormatDecimal(dr["February"]) + FormatDecimal(dr["March"]) + FormatDecimal(dr["April"]) + FormatDecimal(dr["May"]) + FormatDecimal(dr["June"]) + FormatDecimal(dr["July"]) + FormatDecimal(dr["August"]) + FormatDecimal(dr["September"]) + FormatDecimal(dr["October"]) + FormatDecimal(dr["November"]) + FormatDecimal(dr["December"]);

                    dt.Rows.Add(dr);
                }
            }


            //if (dttemp != null && dttemp.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dttemp.Rows.Count; i++)
            //    {
            //        DataRow dr = dt.NewRow();
            //        dr["Price"] = TITLE;
            //        dr["TypeName"] = dttemp.Rows[i]["RAWZSName"];
            //        dr["Name"] = dttemp.Rows[i]["Name"];
            //        dr["Id"] = dttemp.Rows[i]["EatNumber"];
            //        dr["January"] = FormatDecimal(dttemp.Rows[i]["1"]).ToString("F2");
            //        dr["February"] = FormatDecimal(dttemp.Rows[i]["2"]).ToString("F2");
            //        dr["March"] = FormatDecimal(dttemp.Rows[i]["3"]).ToString("F2");
            //        dr["April"] = FormatDecimal(dttemp.Rows[i]["4"]).ToString("F2");
            //        dr["May"] = FormatDecimal(dttemp.Rows[i]["5"]).ToString("F2");
            //        dr["June"] = FormatDecimal(dttemp.Rows[i]["6"]).ToString("F2");
            //        dr["July"] = FormatDecimal(dttemp.Rows[i]["7"]).ToString("F2");
            //        dr["August"] = FormatDecimal(dttemp.Rows[i]["8"]).ToString("F2");
            //        dr["September"] = FormatDecimal(dttemp.Rows[i]["9"]).ToString("F2");
            //        dr["October"] = FormatDecimal(dttemp.Rows[i]["10"]).ToString("F2");
            //        dr["November"] = FormatDecimal(dttemp.Rows[i]["11"]).ToString("F2");
            //        dr["December"] = FormatDecimal(dttemp.Rows[i]["12"]).ToString("F2");
            //        dr["Inventory"] = FormatDecimal(GetXiaohaoResult(dttemp.Rows[i])).ToString("F2");
            //        dr["RawId"] = dttemp.Rows[i]["RAWZS"];
            //        dt.Rows.Add(dr);
            //    }
            //}

            return dt;
        }

        #endregion

        #region 月人均累计百分比
        private decimal GetPersentMonthOfCountAvg(decimal count, decimal number)
        {
            number = (number / count) * 100;
            return number;
        }
        public DataTable GetMonthOfCountAvg(int CompanyID, string AREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.MonthTotal_PercentDataTable dt = new CMS_CateringDataSet.MonthTotal_PercentDataTable();
            DataTable dttemp = dvf.GetMonthOfCountAvg(CLASSID, DTIME);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            listRaws = listRaws.Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6).ToList<Catering_Model.E_RawMaterials>();

            Catering_Rules.OperationArea OAreaRules = new Catering_Rules.OperationArea();
            DataTable dtOArea = null;
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
            {
                dtOArea = OAreaRules.GetList(string.Format(" id = {0}", AREAID)).Tables[0];
            }
            else
            {
                dtOArea = OAreaRules.GetAllListByCompanyID(CompanyID).Tables[0];
            }
            Catering_Rules.ClassInfo ClassRules = new Catering_Rules.ClassInfo();
            DataTable dtClass = classRules.GetAllList().Tables[0];
            var classTemp = from c in dtClass.AsEnumerable()
                            select new
                            {
                                id = Convert.ToInt32(c["id"]),
                                cName = c["CName"].ToString(),
                                areaID = Convert.ToInt32(c["AreaID"])
                            };
            var infotemp = from i in dttemp.AsEnumerable()
                           select new
                           {
                               AreaName = i["AreaName"],
                               CName = i["CName"],
                               TypeName = i["TypeName"],
                               RawID = Convert.ToInt32(i["RawID"]),
                               Numbers = i["Numbers"],
                               ClassID = Convert.ToInt32(i["ClassID"])
                           };
            if (DTIME.Year > 2011)
            {
                for (int i = 0; i < dtOArea.Rows.Count; i++)
                {
                    foreach (var ClassItem in classTemp.Where(p => p.areaID == Convert.ToInt32(dtOArea.Rows[i]["id"])))
                    {
                        decimal count = 0;
                        foreach (var item in listRaws)
                        {
                            if (infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).Count() > 0)
                            {
                                decimal tempA = FormatDecimal(infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).First().Numbers);
                                decimal tempB = FormatDecimal(dvf.GetPersons(ClassItem.id.ToString(), DTIME).Rows[0]["Number"]);
                                decimal infoNum = 0;
                                if (tempA > 0 && tempB > 0)
                                {
                                    infoNum = tempA / tempB;
                                    count += FormatDecimal(infoNum);
                                }
                            }
                        }
                        foreach (var item in listRaws)
                        {
                            DataRow dr = dt.NewRow();
                            dr["AreaName"] = dtOArea.Rows[i]["AreaName"];
                            dr["CName"] = ClassItem.cName;
                            dr["TypeName"] = item.TypeName;
                            dr["Month"] = TITLE;
                            if (infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).Count() > 0 && count > 0)
                            {
                                decimal tempNum = FormatDecimal(infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).First().Numbers);
                                tempNum = FormatDecimal(tempNum / FormatDecimal(dvf.GetPersons(ClassItem.id.ToString(), DTIME).Rows[0]["Number"]));
                                tempNum = FormatDecimal(GetPersentMonthOfCountAvg(count, tempNum));
                                if (tempNum == 0) continue;
                                dr["Numbers"] = tempNum;
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < dtOArea.Rows.Count; i++)
                {
                    foreach (var ClassItem in classTemp.Where(p => p.areaID == Convert.ToInt32(dtOArea.Rows[i]["id"])))
                    {
                        decimal count = 0;
                        foreach (var item in listRaws)
                        {
                            if (infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).Count() > 0)
                            {
                                decimal tempA = FormatDecimal(infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).First().Numbers);
                                decimal infoNum = 0;
                                if (tempA > 0)
                                {
                                    infoNum = tempA;
                                    count += FormatDecimal(infoNum);
                                }
                            }
                        }
                        foreach (var item in listRaws)
                        {
                            DataRow dr = dt.NewRow();
                            dr["AreaName"] = dtOArea.Rows[i]["AreaName"];
                            dr["CName"] = ClassItem.cName;
                            dr["TypeName"] = item.TypeName;
                            dr["Month"] = TITLE;
                            if (infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).Count() > 0 && count > 0)
                            {
                                decimal tempNum = FormatDecimal(infotemp.Where(p => p.RawID == item.id && p.ClassID == ClassItem.id).First().Numbers);
                                tempNum = FormatDecimal(GetPersentMonthOfCountAvg(count, tempNum));
                                if (tempNum == 0) continue;
                                dr["Numbers"] = tempNum;
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            return dt;
        }
        #endregion

        #region 人均食用油统计

        /// <summary>
        /// 获取2011年的人均食用油统计
        /// </summary>
        /// <param name="CompanyID"></param>
        /// <param name="AREAID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="DTIME"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetPersonOilAvgForYear02(int CompanyID, int AREAID, int CLASSID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();
            DataTable dttemp = dvf.GetPersonOilAvg(0, CLASSID, DTIME);
            if (dttemp == null || dttemp.Rows.Count == 0)
            {
                return dt;
            }
            Catering_Rules.OperationArea OAreaRules = new Catering_Rules.OperationArea();
            DataTable dtOArea = null;
            if (AREAID > 0)
            {
                dtOArea = OAreaRules.GetList(string.Format(" id = {0}", AREAID)).Tables[0];
            }
            else
            {
                dtOArea = OAreaRules.GetAllListByCompanyID(CompanyID).Tables[0];
            }
            Catering_Rules.ClassInfo ClassRules = new Catering_Rules.ClassInfo();
            DataTable dtClass = classRules.GetAllList().Tables[0];
            var classTemp = from c in dtClass.AsEnumerable()
                            select new
                            {
                                id = Convert.ToInt32(c["id"]),
                                cName = c["CName"].ToString(),
                                areaID = Convert.ToInt32(c["AreaID"])
                            };
            var infotemp = from i in dttemp.AsEnumerable()
                           select new
                           {
                               AreaName = i["AreaName"],
                               CName = i["CName"],
                               TypeName = i["TypeName"],
                               OutDate = i["OutDate"],
                               RawID = Convert.ToInt32(i["RawID"]),
                               Numbers = i["Numbers"],
                               ClassID = Convert.ToInt32(i["ClassID"])
                           };
            for (int i = 0; i < dtOArea.Rows.Count; i++)
            {
                int areatempid = Convert.ToInt32(dtOArea.Rows[i]["id"]);
                var AreaAvg = FormatDecimal(dvf.GetAreaOilAvg(areatempid.ToString(), DTIME).Rows[0]["PersonAvg"]);

                if (areatempid > 0 && areatempid != 6)
                {
                    int classCount = new Catering_Rules.ClassInfo().GetModelList("").Where(p => p.AreaID == Convert.ToInt32(dtOArea.Rows[i]["id"])).Count();
                    if (classCount != 0)
                    {
                        AreaAvg = FormatDecimal(AreaAvg / classCount);
                    }
                    else
                    {
                        AreaAvg = 0;
                    }
                }

                foreach (var ClassItem in classTemp.Where(p => p.areaID == Convert.ToInt32(dtOArea.Rows[i]["id"])))
                {
                    DataRow dr = dt.NewRow();
                    dr["AreaName"] = dtOArea.Rows[i]["AreaName"];
                    dr["CName"] = ClassItem.cName;
                    dr["Month"] = dtOArea.Rows[i]["id"];
                    dr["Title"] = Title;
                    dr["CMP"] = AreaAvg;
                    for (int ii = 0; ii < dttemp.Rows.Count; ii++)
                    {
                        if (ClassItem.id == Convert.ToInt32(dttemp.Rows[ii]["ClassID"]))
                        {
                            dr["Numbers"] = FormatDecimal(FormatDecimal(dttemp.Rows[ii]["Numbers"]));
                            dr["Eat"] = dttemp.Rows[ii]["Eat"];
                            dr["UsedOil"] = FormatDecimal(FormatDecimal(dttemp.Rows[ii]["UsedOil"]));
                            dr["ExNumber"] = FormatDecimal(FormatDecimal(dttemp.Rows[ii]["Numbers"]) - FormatDecimal(dttemp.Rows[ii]["UsedOil"]));
                            dr["PersonAvg"] = FormatDecimal(dttemp.Rows[ii]["PersonAvg"]) * 1000;
                            break;
                        }
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 获取按年查询的食用油统计
        /// </summary>
        /// <param name="CompanyID"></param>
        /// <param name="StrAreaID"></param>
        /// <param name="StrClassID"></param>
        /// <param name="DTIME"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetPersonOilAvgForYear(int CompanyID, string StrAreaID, string StrClassID, DateTime DTIME, string Title)
        {
            int groupid = 0;
            int classid = 0;

            Int32.TryParse(StrAreaID, out groupid);
            Int32.TryParse(StrClassID, out classid);

            if (DTIME.Year > 2011) return GetPersonOilAvgForYear01(CompanyID, groupid, classid, DTIME, Title);
            return GetPersonOilAvgForYear02(CompanyID, groupid, classid, DTIME, Title);
        }

        /// <summary>
        /// 获取按年查询>2011年的食用油统计
        /// </summary>
        /// <param name="CompanyID"></param>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <param name="DTIME"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetPersonOilAvgForYear01(int CompanyID, int groupid, int classid, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();


            //获取班组及作业区信息
            string classwhere = "";
            if (CompanyID > 0) classwhere += " AND CompanyID=" + CompanyID + " ";
            if (groupid > 0) classwhere += " AND AreaID=" + groupid + "";
            if (classid > 0) classwhere += " AND ClassID=" + classid + " ";
            DataTable dt_class = new Catering_Rules.Company().GetView_CompanyList(classwhere).Tables[0];

            if (dt_class != null && dt_class.Rows.Count > 0)
            {
                var grouplist = from c in dt_class.AsEnumerable()
                                group c by new { t1 = c.Field<int>("AreaID"), t2 = c.Field<string>("AreaName") } into t
                                select new
                                {
                                    areaid = t.Key.t1,
                                    areaname = t.Key.t2
                                };
                grouplist.ToList().ForEach(g =>
                {
                    string gname = g.areaname;
                    int gid = g.areaid;
                    var gclasslist = from c in dt_class.AsEnumerable()
                                     where c.Field<int>("AreaID") == gid
                                     select new
                                     {
                                         gclassname = c.Field<string>("CName"),
                                         gclassid = c.Field<int>("ClassID")
                                     };


                    gclasslist.ToList().ForEach(c =>
                    {
                        int cid = c.gclassid;
                        string cname = c.gclassname;
                        DataRow dr = dt.NewRow();
                        dr["AreaName"] = gname;
                        dr["Month"] = gid;
                        dr["Title"] = Title;
                        dr["CMP"] = Toolss.GetMathResult(dvf.GetGroupPersonOilByYear(gid, DTIME), 0, 3);//作业区人均消耗量
                        dr["CName"] = cname;
                        DataTable dttemp = dvf.GetPersonOilAvg(gid, cid, DTIME);
                        for (int ii = 0; ii < dttemp.Rows.Count; ii++)
                        {
                            if (cid == Convert.ToInt32(dttemp.Rows[ii]["ClassID"]))
                            {
                                decimal numbers = FormatDecimal(dttemp.Rows[ii]["Numbers"]);//消耗量，单位：g
                                int eat = Convert.ToInt32(dttemp.Rows[ii]["EatNumber"]);//就餐人数：定员+其他+配餐在岗人员
                                dr["Numbers"] = Toolss.GetMathResult(numbers / 1000, 2, 3);//消耗量，单位：kg
                                dr["Eat"] = eat;
                                dr["UsedOil"] = eat * Convert.ToDecimal(0.03);//标准用油，单位：kg
                                dr["ExNumber"] = Toolss.GetMathResult(numbers / 1000, 2, 3) - (eat * Convert.ToDecimal(0.03));//超量，单位：kg
                                dr["PersonAvg"] = Toolss.GetMathResult(numbers / eat, 2, 3);//人均消耗量，单位：g
                                break;
                            }
                        }
                        dttemp.Dispose();
                        dt.Rows.Add(dr);
                    }
                    );
                });
                dt_class.Dispose();
            }

            return dt;
        }


        public DataTable GetPersonOilAvgGroupByClassID(string pid, string AREAID, string CLASSID, DateTime DTIME)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dt = dvf.GerPersonOilAvgGroupByClassID(strarealist, AREAID, CLASSID, DTIME);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return dt;
            }
            DataTable redt = new DataTable();
            redt.Columns.Add("ClassID", typeof(int));//增加字段ClassID
            redt.Columns.Add("CName", typeof(string));//增加字段CName
            redt.Columns.Add("AvgOil", typeof(string));//增加字段ClassID
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int classid = Convert.ToInt32(dt.Rows[i]["ID"]);
                string className = dt.Rows[i]["CName"].ToString();
                decimal SumNum = Convert.ToDecimal(dt.Rows[i]["SumNumber"]);
                decimal personNum = 1;
                if (!(dt.Rows[i]["PersonNum"] is DBNull)) personNum = Convert.ToDecimal(dt.Rows[i]["PersonNum"]);
                DataRow dr = redt.NewRow();
                dr["ClassID"] = classid;
                dr["CName"] = className;
                dr["AvgOil"] = GetMathResult(SumNum / personNum, 0, 3);
                redt.Rows.Add(dr);
            }
            dt.Dispose();
            return redt;
        }

        public DataTable GerPersonOilAvgGroupByGroupID(string pid, string AREAID, DateTime DTIME)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dt = dvf.GerPersonOilAvgGroupByGroupID(strarealist, AREAID, DTIME);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return dt;
            }
            DataTable redt = new DataTable();
            redt.Columns.Add("AreaID", typeof(int));//增加字段ClassID
            redt.Columns.Add("AreaName", typeof(string));//增加字段CName
            redt.Columns.Add("AvgOil", typeof(string));//增加字段ClassID
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int areaid = Convert.ToInt32(dt.Rows[i]["AreaID"]);
                string areaName = dt.Rows[i]["AreaName"].ToString();
                decimal SumNum = Convert.ToDecimal(dt.Rows[i]["SumNumber"]);
                decimal personNum = 1;
                if (!(dt.Rows[i]["PersonNum"] is DBNull)) personNum = Convert.ToDecimal(dt.Rows[i]["PersonNum"]);
                DataRow dr = redt.NewRow();
                dr["AreaID"] = areaid;
                dr["AreaName"] = areaName;
                dr["AvgOil"] = GetMathResult(SumNum / personNum, 0, 3);
                redt.Rows.Add(dr);
            }
            dt.Dispose();
            return redt;
        }

        #endregion

        #region 报表导出数据
        /// <summary>
        /// 报表导出数据
        /// </summary>
        /// <param name="rv">报表控件</param>
        /// <param name="savePath">保存地址</param>
        public void SaveAsExcelByReportView(ReportViewer rv, string savePath)
        {
            Warning[] warnings;

            string[] streamids;

            string mimeType;

            string encoding;

            string extension;

            byte[] bytes = rv.LocalReport.Render(

            "Excel", null, out mimeType, out encoding, out extension,

            out streamids, out warnings);

            FileStream fs = new FileStream(savePath, FileMode.Create);

            fs.Write(bytes, 0, bytes.Length);

            fs.Close();
        }
        #endregion

        #region 人均分类对比
        public void GetPreson_Avg_Contrast(string pid, string AREAID, string[] year, string classid, int[] rawid, out DataTable frdt, out DataTable dynamicdt)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            frdt = new DataViewForReport().GetPerson_Avg_ContrastByYear(strarealist, year, AREAID, classid, rawid);//分类消耗
            dynamicdt = new Catering_Rules.Dynamic().GetEat_Of_YearByTime(strarealist, year, AREAID, classid);//配餐人数
        }
        #endregion

        #region 人均消耗统计
        public DataTable GetPersonAvgXiaohao(string pid, string classid, string groupid, string year, string month, string Title)
        {
            DataTable dt = new CMS_CateringDataSet.Person_Avg_XiaohaoDataTable();
            Catering_Rules.DataViewForReport dvfrRules = new DataViewForReport();
            Catering_Rules.Dynamic dynamicRules = new Catering_Rules.Dynamic();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            List<Catering_Model.RawMaterialsType> listRaws = new Catering_Rules.RawMaterialsType().GetModelList("").Where(p => p.id != 1 && p.id != 9 && p.id != 5 && p.id != 12 && p.id != 2 && p.id != 10 && p.id != 11).ToList<Catering_Model.RawMaterialsType>();
            DataTable frdt = dvfrRules.GetRenJunXHTongjiByYear(year, strarealist, groupid, classid, null);
            if (Convert.ToInt32(year) > 2011)
            {
                foreach (var itemRaw in listRaws)
                {
                    for (int iMonth = 1; iMonth <= 12; iMonth++)
                    {
                        decimal dynamicdt = dynamicRules.GetEatNumberByMonth(classid, year, iMonth.ToString());
                        DataRow dr = dt.NewRow();
                        dr["TypeName"] = itemRaw.Name;
                        dr["Month"] = iMonth;
                        dr["Title"] = Title;
                        dr["EatNumber"] = dynamicdt;
                        decimal number = 0;
                        for (int i = 0; i < frdt.Rows.Count; i++)
                        {
                            //dr["Month"] = frdt.Rows[i]["chmonth"].ToString();
                            if (frdt.Rows[i]["numMonth"].ToString().Equals(iMonth.ToString()) && itemRaw.id == Convert.ToInt32(frdt.Rows[i]["RAWTID"]))
                            {
                                Decimal temp = 0;
                                decimal.TryParse(frdt.Rows[i]["SumNumber"].ToString(), out temp);
                                number += temp;
                            }
                        }
                        if (number > 0 && dynamicdt > 0)
                        {
                            dr["Number"] = GetMathResult(number / dynamicdt, 0, 3);
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            else
            {
                var ListModel = new Catering_Rules.T_RawMaterials().GetModelList("").Where(p => p.id == 1 || p.id == 2 || p.id == 3 || p.id == 5 || p.id == 6);
                if (frdt != null && frdt.Rows.Count > 0)
                {
                    foreach (var itemRaw in ListModel)
                    {
                        for (int iMonth = 1; iMonth <= 12; iMonth++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["TypeName"] = itemRaw.TypeName;
                            dr["Month"] = iMonth;
                            dr["Title"] = Title;
                            for (int i = 0; i < frdt.Rows.Count; i++)
                            {
                                if (frdt.Rows[i]["numMonth"].ToString().Equals(iMonth.ToString()) && itemRaw.id == Convert.ToInt32(frdt.Rows[i]["RAWTID"]))
                                {
                                    dr["EatNumber"] = FormatDecimal(frdt.Rows[i]["EatNumber"]);
                                    dr["Number"] = FormatDecimal(frdt.Rows[i]["Numbers"]);
                                }
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            return dt;
        }
        #endregion

        #region 人均分类统计
        /// <summary>
        /// 人均分类统计
        /// </summary>
        /// <returns></returns>
        public DataTable GetPersonAvgContrastForLineChar(string pid, string year, string areaid, string classId, int[] rawId)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Number", typeof(decimal));//增加字段Number
            dt.Columns.Add("TypeName", typeof(string));//增加字段TypeName
            dt.Columns.Add("Month", typeof(int));//增加字段Month
            dt.Columns.Add("NumYear", typeof(int));//增加字段NumYear
            string strclasslist = "";
            if (!string.IsNullOrEmpty(areaid) && areaid != "-10")
            {
                strclasslist = new Catering_Rules.ClassInfo().GetStrtClassIDListByGroupID(areaid);
            }
            else
            {
                strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            }
            if (Convert.ToInt32(year) > 2011)
            {
                #region 大于2011年的正常取数
                DataTable dydt = new Catering_Rules.Dynamic().GetEatNumbersByYear(strclasslist, year, classId);
                DataTable tempdt = new Catering_Rules.DataViewForReport().GetPersonAvgContrastForLineChar(strclasslist, year, classId, rawId);
                List<Catering_Model.RawMaterialsType> ListRawsTemp = new Catering_Rules.RawMaterialsType().GetModelList("");
                List<Catering_Model.RawMaterialsType> ListRaws = new List<Catering_Model.RawMaterialsType>();
                if (rawId == null || rawId.Count() == 0)
                {
                    ListRaws = ListRawsTemp;
                }
                else
                {
                    for (int i = 0; i < rawId.Count(); i++)
                    {
                        var RawsModel = ListRawsTemp.Where(p => p.id == Convert.ToInt32(rawId[i])).Count() > 0 ? ListRawsTemp.Where(p => p.id == Convert.ToInt32(rawId[i])).First() : null;
                        if (RawsModel != null)
                        {
                            ListRaws.Add(RawsModel);
                        }
                    }
                }

                foreach (var Rawsitem in ListRaws)
                {
                    for (int iMonth = 1; iMonth <= 12; iMonth++)
                    {
                        for (int i = 0; i < dydt.Rows.Count; i++)
                        {
                            if (!dydt.Rows[i]["nummonth"].Equals(iMonth)) continue;
                            for (int ii = 0; ii < tempdt.Rows.Count; ii++)
                            {
                                if (dydt.Rows[i]["nummonth"].Equals(tempdt.Rows[ii]["mon"]) && Rawsitem.id.Equals(Convert.ToInt32(tempdt.Rows[ii]["RAWTID"])))
                                {
                                    if (FormatDecimal(dydt.Rows[i]["number"]) > 0 && FormatDecimal(tempdt.Rows[ii]["number"]) > 0)
                                    {
                                        DataRow dr = dt.NewRow();
                                        dr["Month"] = iMonth;
                                        dr["TypeName"] = Rawsitem.Name;
                                        if (Rawsitem.Name == "油")
                                        {
                                            dr["Number"] = Decimal.Round(FormatDecimal(tempdt.Rows[ii]["number"]) / FormatDecimal(dydt.Rows[i]["number"]));
                                        }
                                        else
                                        {
                                            dr["Number"] = Decimal.Round(FormatDecimal(tempdt.Rows[ii]["number"]) / FormatDecimal(dydt.Rows[i]["number"]));
                                        }
                                        dr["NumYear"] = Convert.ToInt32(tempdt.Rows[ii]["year"]);
                                        dt.Rows.Add(dr);
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                #endregion
            }
            else
            {
                DataTable tempdt = new Catering_Rules.DataViewForReport().GetPersonAvgContrastForLineChar(strclasslist, year, classId, rawId);
                for (int ii = 0; ii < tempdt.Rows.Count; ii++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Month"] = tempdt.Rows[ii]["mon"];
                    dr["TypeName"] = EXChangeRAW(tempdt.Rows[ii]["TypeName"].ToString());
                    dr["Number"] = FormatDecimal(tempdt.Rows[ii]["number"]);
                    dr["NumYear"] = tempdt.Rows[ii]["year"];
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion

        #region 人均食用油汇总

        /// <summary>
        /// 大于2011年的食用油消耗汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title">标题</param>
        /// <returns></returns>
        public DataTable GetOilPersonAvgContrantCurrent(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();
            string areawhere = "";
            string classwhere = "";
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            if (!AREAID.Equals("-10"))
            {
                areawhere = string.Format(" ID = {0} ", AREAID);
                if (!CLASSID.Equals("-10")) classwhere = string.Format(" ID = {0} ", CLASSID);
            }
            else
            {
                areawhere = string.Format(" ID IN ({0})", strarealist);
            }
            List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetModelList(classwhere);
            List<Catering_Model.OperationArea> listArea = new Catering_Rules.OperationArea().GetModelList(areawhere);
            DataTable dt_xiahao = dvf.GetXiaoHaoAvg(strarealist, AREAID, CLASSID, DTIME.Year);
            DataTable dt_caigou = dvf.GetPurchase(strarealist, AREAID, CLASSID, DTIME.Year);
            DataTable dt_dynamic = dvf.GetDynamic(strarealist, AREAID, CLASSID, DTIME.Year);
            string[] strmonth = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            foreach (Catering_Model.OperationArea itemArea in listArea)
            {
                foreach (Catering_Model.ClassInfo itemClass in listClass.Where(p => p.AreaID == itemArea.id))
                {
                    DataRow dr = dt.NewRow();
                    dr["AreaName"] = itemArea.AreaName;
                    dr["CName"] = itemClass.CName;
                    dr["CMP"] = Title;
                    dr["GroupID"] = itemArea.id;
                    dr["ClassID"] = itemClass.id;
                    var v_xh = from x in dt_xiahao.AsEnumerable()
                               where x.Field<int>("GroupID") == itemArea.id && x.Field<int>("ClassID") == itemClass.id
                               select new
                               {
                                   GroupID = x.Field<int>("GroupID"),
                                   ClassID = x.Field<int>("ClassID"),
                                   SumNumber = x.Field<decimal>("SumNumber"),
                                   Month = x.Field<int>("Month")
                               };
                    var v_caigou = from x in dt_caigou.AsEnumerable()
                                   where x.Field<int>("GroupID") == itemArea.id && x.Field<string>("ClassID") == itemClass.id.ToString()
                                   select new
                                   {
                                       GroupID = x.Field<int>("GroupID"),
                                       ClassID = x.Field<string>("ClassID"),
                                       GrogramNumber = x.Field<decimal>("GrogramNumber"),
                                       Month = x.Field<int>("Month")
                                   };
                    var v_no = from x in dt_dynamic.AsEnumerable()
                               where x.Field<int>("GroupID") == itemArea.id && x.Field<int>("ClassID") == itemClass.id
                               select new
                               {
                                   GroupID = x.Field<int>("GroupID"),
                                   ClassID = x.Field<int>("ClassID"),
                                   Number = x.Field<int>("Number"),
                                   Month = x.Field<int>("Month")
                               };
                    decimal xhsum = 0;
                    decimal cgsum = 0;
                    int xhcount = 0;
                    int cgcount = 0;
                    for (int i = 0; i < 12; i++)
                    {
                        decimal xhnum = 0;
                        decimal cgnum = 0;
                        int number = 0;
                        try { xhnum = v_xh.ToList().Where(x => x.Month == (i + 1)).Select(x => x.SumNumber).First(); } catch { }
                        try { cgnum = v_caigou.ToList().Where(x => x.Month == (i + 1)).Select(x => x.GrogramNumber).First(); } catch { }
                        try { number = v_no.ToList().Where(x => x.Month == (i + 1)).Select(x => x.Number).First(); } catch { }
                        if (xhnum > 0)
                        {
                            xhsum += xhnum;
                            xhcount += number;
                        }
                        if (cgnum > 0)
                        {
                            cgsum += cgnum;
                            cgcount += number;
                        }
                        number = number > 0 ? number : 1;
                        dr[strmonth[i] + "r"] = GetMathResult(xhnum / number, 0, 3);
                        dr[strmonth[i] + "c"] = GetMathResult(cgnum / number, 0, 3);
                    }
                    xhcount = xhcount > 0 ? xhcount : 1;
                    cgcount = cgcount > 0 ? cgcount : 1;
                    dr["CountXiaohao"] = xhsum > 0 ? GetMathResult(xhsum / xhcount, 0, 3) : 0;
                    dr["CountYaoliao"] = cgsum > 0 ? GetMathResult(cgsum / cgcount, 0, 3) : 0;
                    dt.Rows.Add(dr);
                }
            }
            dt_xiahao.Dispose();
            dt_caigou.Dispose();
            dt_dynamic.Dispose();
            return dt;
        }

        /// <summary>
        /// 2011年以前的食用油消耗汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title">标题</param>
        /// <returns></returns>
        public DataTable GetOilPersonAvgContrantHistory(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();
            string areawhere = "";
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            if (!AREAID.Equals("-10"))
                areawhere = string.Format(" ID = {0} ", AREAID);
            else
                areawhere = string.Format(" ID IN ({0})", strarealist);
            List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetModelList("");
            List<Catering_Model.OperationArea> listArea = new Catering_Rules.OperationArea().GetModelList(areawhere);
            DataTable dttemp = dvf.GetOilPersonAvgHistoryContrant(strarealist, AREAID, CLASSID, DTIME.Year.ToString(), "");
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                foreach (var itemArea in listArea)
                {
                    foreach (var itemClass in listClass.Where(p => p.AreaID == itemArea.id))
                    {
                        for (int i = 0; i < dttemp.Rows.Count; i++)
                        {
                            if (itemClass.id.ToString() == dttemp.Rows[i]["ClassID"].ToString())
                            {
                                DataRow dr = dt.NewRow();
                                dr["AreaName"] = dttemp.Rows[i]["AreaName"];
                                dr["CName"] = dttemp.Rows[i]["CName"];
                                dr["CMP"] = Title;
                                dr["GroupID"] = dttemp.Rows[i]["GroupID"];
                                dr["ClassID"] = dttemp.Rows[i]["ClassID"];
                                dr["Januaryr"] = FormatDecimal(dttemp.Rows[i]["1r"]);

                                dr["Februaryr"] = FormatDecimal(dttemp.Rows[i]["2r"]);

                                dr["Marchr"] = FormatDecimal(dttemp.Rows[i]["3r"]);

                                dr["Aprilr"] = FormatDecimal(dttemp.Rows[i]["4r"]);

                                dr["Mayr"] = FormatDecimal(dttemp.Rows[i]["5r"]);

                                dr["Juner"] = FormatDecimal(dttemp.Rows[i]["6r"]);

                                dr["Julyr"] = FormatDecimal(dttemp.Rows[i]["7r"]);

                                dr["Augustr"] = FormatDecimal(dttemp.Rows[i]["8r"]);

                                dr["Septemberr"] = FormatDecimal(dttemp.Rows[i]["9r"]);

                                dr["Octoberr"] = FormatDecimal(dttemp.Rows[i]["10r"]);

                                dr["Novemberr"] = FormatDecimal(dttemp.Rows[i]["11r"]);

                                dr["Decemberr"] = FormatDecimal(dttemp.Rows[i]["12r"]);

                                decimal CountXiaohao = GetOilCountXiaohao(dttemp.Rows[i], "r");

                                dr["CountXiaohao"] = CountXiaohao > 0 ? GetMathResult(CountXiaohao, 0, 3) : 0;

                                dt.Rows.Add(dr);
                            }
                        }
                    }
                }
            }
            dttemp.Dispose();
            return dt;
        }

        /// <summary>
        /// 食用油消耗汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title">标题</param>
        /// <returns></returns>
        public DataTable GetOilPersonAvgContrant(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            if (DTIME.Year > 2011) return GetOilPersonAvgContrantCurrent(pid, AREAID, CLASSID, DTIME, Title);
            return GetOilPersonAvgContrantHistory(pid, AREAID, CLASSID, DTIME, Title);
        }

        private decimal GetOilCountXiaohao(DataRow dr, string str)
        {
            decimal count = 0;
            for (int i = 0; i < 12; i++)
            {
                count += FormatDecimal(dr[i + 1 + str]);
            }
            return count;
        }


        /// <summary>
        /// 大于2011年的食用油消耗汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title">标题</param>
        /// <returns></returns>
        public DataTable GetOilPersonAvgForChar(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            //DataTable dttemp = dvf.GetOilPersonAvgForChar(strarealist, AREAID, CLASSID, DTIME.Year.ToString());
            //var listTemp = from d in dttemp.AsEnumerable()
            //               select new
            //               {
            //                   Numbers = Convert.ToInt32(d["Numbers"]),
            //                   Month = Convert.ToInt32(d["nummonth"])
            //               };
            //if (dttemp != null && dttemp.Rows.Count > 0)
            //{
            //    for (int i = 1; i <= 12; i++)
            //    {
            //        var listItems = listTemp.Where(p => p.Month == i).ToList();
            //        if (listItems != null && listItems.Count > 0)
            //        {
            //            foreach (var item in listItems)
            //            {
            //                DataRow dr = dt.NewRow();
            //                dr["Month"] = i;
            //                dr["CMP"] = Title;
            //                dr["Numbers"] = item.Numbers;
            //                dt.Rows.Add(dr);
            //            }
            //        }
            //        else
            //        {
            //            DataRow dr = dt.NewRow();
            //            dr["Month"] = i;
            //            dr["CMP"] = Title;
            //            dt.Rows.Add(dr);
            //        }
            //    }
            //}
            return dt;
        }

        /// <summary>
        /// 食用油汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetOilPersonAvg(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            if (DTIME.Year > 2011) return GetOilPersonAvgForChar(pid, AREAID, CLASSID, DTIME, Title);
            return GetOilPersonAvgHistoryForChar(pid, AREAID, CLASSID, DTIME, Title);

        }

        /// <summary>
        /// 2011年以前的食用油消耗汇总
        /// </summary>
        /// <param name="pid">用户ID</param>
        /// <param name="AREAID">作业区</param>
        /// <param name="CLASSID">班组</param>
        /// <param name="DTIME">时间</param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public DataTable GetOilPersonAvgHistoryForChar(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title)
        {
            CMS_CateringDataSet.PersonEdible_StatisticsDataTable dt = new CMS_CateringDataSet.PersonEdible_StatisticsDataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetOilPersonAvgHistoryForChar(strarealist, AREAID, CLASSID, DTIME.Year.ToString());
            var listTemp = from d in dttemp.AsEnumerable()
                           select new
                           {
                               Numbers = Convert.ToInt32(d["Numbers"]),
                               Month = Convert.ToInt32(d["nummonth"])
                           };
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                for (int i = 1; i <= 12; i++)
                {
                    var listItems = listTemp.Where(p => p.Month == i).ToList();
                    if (listItems != null && listItems.Count > 0)
                    {
                        foreach (var item in listItems)
                        {
                            DataRow dr = dt.NewRow();
                            dr["Month"] = i;
                            dr["CMP"] = Title;
                            dr["Numbers"] = item.Numbers;
                            dt.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        dr["Month"] = i;
                        dr["CMP"] = Title;
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }
        #endregion

        #region 成本比例
        /// <summary>
        /// 根本班组和时间查询，参数都可为空值，空值情况下为全部查询。
        /// </summary>
        /// <param name="classId">班组ID</param>
        /// <param name="timeType">时间类型，年、月</param>
        /// <param name="time">需要一个完整的DateTime类型</param>
        /// <returns></returns>
        public DataTable GetCostDataForPieChar(string pid, string areaid, string classId, string timeType, DateTime time, string TITLE)
        {
            try
            {
                string where = "";

                if (!areaid.Equals("-10"))
                {
                    where += string.Format(" and GroupID = {0} ", areaid);
                    if (!classId.Equals("-10"))
                    {
                        where += string.Format(" and ClassID = {0}", classId);
                    }
                }
                else
                {
                    string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                    if (!string.IsNullOrEmpty(strarealist)) where += string.Format(" and GroupID IN ({0})", strarealist);
                }

                if (!string.IsNullOrEmpty(timeType) && time != null)
                {
                    switch (timeType)
                    {
                        case "Year":
                            where += string.Format(" and OutDate BETWEEN Convert(Datetime,'{0}') and Convert(Datetime,'{1}') ", time.Year + "-01-01 00:00:00", time.Year + "-12-31 23:59:59");
                            break;
                        case "Month":
                            where += string.Format(" and month(OutDate) = {0} and year(outdate) = {1}", time.Month, time.Year);
                            break;
                        default:
                            break;
                    }
                }
                CMS_CateringDataSet.DTCostPieCharDataTable dt = new CMS_CateringDataSet.DTCostPieCharDataTable();
                DataTable temp = new Catering_Rules.DataViewForReport().GetViewFromRecords(where);
                DataTable tempraw = new Catering_Rules.T_RawMaterials().GetAllList().Tables[0];
                for (int i = 0; i < tempraw.Rows.Count; i++)
                {
                    var v = from r in temp.AsEnumerable()
                            where r["RAWTID"].Equals(tempraw.Rows[i]["id"])
                            select new
                            {
                                Numbers = r["Numbers"].ToString(),
                                Price = r["Price"].ToString(),
                                RAWTID = r["RAWTID"].ToString(),
                                TypeName = r["TypeName"].ToString(),
                                CName = r["CName"].ToString(),
                                ClassID = r["ClassID"].ToString()
                            };
                    if (v != null && v.Count() > 0)
                    {
                        decimal num = 0;
                        decimal price = 0;
                        decimal count = 0;
                        foreach (var item in v)
                        {
                            num = Convert.ToDecimal(item.Numbers);
                            price = Convert.ToDecimal(item.Price);
                            count += num * price;
                        }
                        DataRow dr = dt.NewRow();
                        dr["TypeName"] = v.First().TypeName;
                        dr["ClassName"] = TITLE;
                        dr["Number"] = count;
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region 月人均成本明细
        public DataTable GetCostDataPersonAvg(string pid, string oAreaId, string classId, DateTime time, bool isZiYingCompany)
        {
            CMS_CateringDataSet.DTCostDataPersonAvgDataTable dt = new CMS_CateringDataSet.DTCostDataPersonAvgDataTable();
            Catering_Model.ClassInfo classInfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classId));
            Catering_Model.OperationArea areaInfo = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(oAreaId));
            DataTable classdt = null;
            string Title = time.Year + "年";
            if (areaInfo != null && !string.IsNullOrEmpty(areaInfo.AreaName))
            {
                Title += areaInfo.AreaName;
                if (classInfo != null && !string.IsNullOrEmpty(classInfo.CName))
                {
                    Title += classInfo.CName;
                }
                else
                {
                    Title += "各班组";
                }
            }
            else
            {
                Title += "所有作业区";
            }
            Title += "人均伙食成本平均值";
            if (oAreaId.Equals("-10") && classId.Equals("-10"))
            {
                string arealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                if (!string.IsNullOrEmpty(arealist)) classdt = new Catering_Rules.ClassInfo().GetList(" AreaID IN (" + arealist + ") ").Tables[0];
            }
            else if (!string.IsNullOrEmpty(oAreaId) && !oAreaId.Equals("-10") && classId.Equals("-10"))
            {
                classdt = new Catering_Rules.ClassInfo().GetList("AreaID = " + oAreaId).Tables[0];
            }
            else
            {
                classdt = new Catering_Rules.ClassInfo().GetList("id = " + classId).Tables[0];
            }

            DataTable frdt = new Catering_Rules.FromRecords().FromRecordsListGroupByMonth(oAreaId, classId, time).Tables[0];//各班组个月的总金额
            DataTable drdt = new Catering_Rules.Dynamic().DynamicListGroupByMonth(oAreaId, classId, time);//各班组每月的总人数
            for (int i = 0; i < classdt.Rows.Count; i++)
            {
                string classid = classdt.Rows[i]["id"].ToString();//班组
                string className = classdt.Rows[i]["CName"].ToString();
                if (!isZiYingCompany && className == "自营公司") continue;
                for (int ii = 1; ii <= 12; ii++)
                {
                    DataRow tempdr = dt.NewRow();
                    tempdr["ClassName"] = classdt.Rows[i]["CName"];
                    tempdr["Month"] = ii;
                    tempdr["Title"] = Title;

                    #region 年人均合计

                    decimal frMoeny = FormatDecimal(new Catering_Rules.FromRecords().FromRecordsMoneyCount(classid, time).Rows[0]["amountmoney"]);
                    decimal drPersons = FormatDecimal(new Catering_Rules.Dynamic().GetEatNumbersCount(classid, time).Rows[0]["dnumber"]);
                    if (frMoeny > 0 && drPersons > 0)
                    {
                        tempdr["id"] = Toolss.GetMathResult(frMoeny / drPersons, 1, 3);
                    }
                    else if (frMoeny > 0 || drPersons > 0)
                    {
                        tempdr["id"] = 0;
                    }

                    #endregion

                    decimal zongrenshu = 0;
                    decimal zongjine = 0;
                    for (int iii = 0; iii < frdt.Rows.Count; iii++)
                    {
                        string tempclassid = frdt.Rows[iii]["ClassID"].ToString();
                        if (ii == Convert.ToInt32(frdt.Rows[iii]["fmonth"].ToString()) && classid.Equals(tempclassid))
                        {
                            zongjine = FormatDecimal(frdt.Rows[iii]["amountmoney"]);
                            break;
                        }
                    }
                    for (int iii = 0; iii < drdt.Rows.Count; iii++)
                    {
                        string tempclassid = drdt.Rows[iii]["ClassID"].ToString();
                        if (ii == Convert.ToInt32(drdt.Rows[iii]["monthofdate"].ToString()) && classid.Equals(tempclassid))
                        {
                            zongrenshu = FormatDecimal(drdt.Rows[iii]["dnumber"]);
                            break;
                        }
                    }
                    tempdr["Numbers"] = "";
                    if (zongrenshu > 0 && zongjine > 0)
                    {
                        tempdr["Numbers"] = Toolss.GetMathResult(zongjine / zongrenshu, 1, 3);
                    }
                    else if (zongrenshu > 0 || zongjine > 0)
                    {
                        tempdr["Numbers"] = 0;
                    }
                    dt.Rows.Add(tempdr);
                }
            }
            frdt.Dispose();
            drdt.Dispose();
            return dt;
        }
        #endregion

        #region 消耗分类横向对比
        public DataTable GetXiaohaoTypeCrossChar(int[] rawTIDs, ArrayList classIds, string[] years)
        {
            return dvf.GetXiaohaoTypeCrossChar(rawTIDs, classIds, years);
        }
        #endregion

        #region 消耗分类纵向对比

        public DataTable GetXiaohaoTypePortait(int[] RAWTID, string CLASSID, DateTime DTIME)
        {
            CMS_CateringDataSet.View_XiaoHaoSumDataTable dt = new CMS_CateringDataSet.View_XiaoHaoSumDataTable();
            DataTable temp = dvf.GetXiaohaoTypePortait(RAWTID, CLASSID, DTIME);
            if (temp != null && temp.Rows.Count > 0)
            {
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["TypeName"] = temp.Rows[i]["TypeName"];
                    dr["January"] = temp.Rows[i]["1"];
                    dr["February"] = temp.Rows[i]["2"];
                    dr["March"] = temp.Rows[i]["3"];
                    dr["April"] = temp.Rows[i]["4"];
                    dr["May"] = temp.Rows[i]["5"];
                    dr["June"] = temp.Rows[i]["6"];
                    dr["July"] = temp.Rows[i]["7"];
                    dr["August"] = temp.Rows[i]["8"];
                    dr["September"] = temp.Rows[i]["9"];
                    dr["October"] = temp.Rows[i]["10"];
                    dr["November"] = temp.Rows[i]["11"];
                    dr["December"] = temp.Rows[i]["12"];
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 第三方折线图
        /// </summary>
        /// <param name="RAWTID"></param>
        /// <param name="CLASSID"></param>
        /// <param name="AREAID"></param>
        /// <param name="CompanyID"></param>
        /// <param name="DTIME"></param>
        /// <returns></returns>
        public DataTable GetXiaohaoTypePortaitThird(int[] RAWTID, string CLASSID, string AREAID, string CompanyID, DateTime DTIME)
        {
            return dvf.GetXiaohaoTypePortaitThird(RAWTID, CLASSID, AREAID, CompanyID, DTIME);
        }

        #endregion

        #region 年度伙食统计表
        public DataTable GetHallYearAte(string OAREAID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.DTCostDataPersonAvgDataTable dt = new CMS_CateringDataSet.DTCostDataPersonAvgDataTable();

            DataTable classdt = new DataTable();
            if (OAREAID.Equals("-10") && CLASSID.Equals("-10"))
            {
                classdt = new Catering_Rules.ClassInfo().GetAllList().Tables[0];
            }
            else if (!string.IsNullOrEmpty(OAREAID) && !OAREAID.Equals("-10") && CLASSID.Equals("-10"))
            {
                classdt = new Catering_Rules.ClassInfo().GetList("AreaID = " + OAREAID).Tables[0];
            }
            else
            {
                classdt = new Catering_Rules.ClassInfo().GetList("id = " + CLASSID).Tables[0];
            }
            DataTable frdt = dvf.GetHallYearAte(OAREAID, CLASSID, DTIME);
            for (int i = 0; i < classdt.Rows.Count; i++)
            {
                if (classdt.Rows[i]["CName"].ToString() == "自营公司") continue;
                for (int ii = 1; ii <= 12; ii++)
                {
                    DataRow tempdr = dt.NewRow();
                    tempdr["ClassName"] = classdt.Rows[i]["CName"];
                    tempdr["Month"] = ii;
                    tempdr["Title"] = TITLE;
                    for (int iii = 0; iii < frdt.Rows.Count; iii++)
                    {
                        if (ii == Convert.ToInt32(frdt.Rows[iii]["numMonth"].ToString()) && classdt.Rows[i]["id"].ToString().Equals(frdt.Rows[iii]["ClassID"].ToString()))
                        {
                            tempdr["Numbers"] = frdt.Rows[iii]["Numbers"];
                        }
                    }
                    dt.Rows.Add(tempdr);
                }
            }
            return dt;
        }
        #endregion

        #region 人工成本

        public DataTable GetLaborCostsList(string OAREAID, string CLASSID, DateTime DTIME, string CompanyID, string TITLE)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Numbers", typeof(decimal));//增加字段Numbers
            dt.Columns.Add("ClassName", typeof(string));//增加字段ClassName
            dt.Columns.Add("Month", typeof(int));//增加字段Month
            dt.Columns.Add("Title", typeof(string));//增加字段Title
            dt.Columns.Add("No", typeof(string));//增加字段No
            DataTable tempdt = dvf.GetLaborCostsList(OAREAID, CLASSID, DTIME, CompanyID);
            DataTable classdt = new Catering_Rules.ClassInfo().GetModelList(OAREAID, CLASSID, CompanyID);

            if (classdt != null && classdt.Rows.Count > 0)
            {
                int n = 1;
                for (int i = 0; i < classdt.Rows.Count; i++)
                {
                    int classid = Convert.ToInt32(classdt.Rows[i]["ClassID"]);
                    int groupid = Convert.ToInt32(classdt.Rows[i]["AreaID"]);
                    string className = classdt.Rows[i]["CName"].ToString();
                    int num = new Catering_Rules.FromRecords().FromRecordsMoney(classid.ToString(), DTIME);
                    for (int iMonth = 1; iMonth <= 12; iMonth++)
                    {
                        bool flag = false;
                        for (int j = 0; j < tempdt.Rows.Count; j++)
                        {
                            DateTime date = Convert.ToDateTime(tempdt.Rows[j]["Date"]);
                            if (date.Month == iMonth && Convert.ToInt32(tempdt.Rows[j]["ClassID"]) == classid)
                            {

                                DataRow dr = dt.NewRow();
                                dr["Numbers"] = Convert.ToDecimal(tempdt.Rows[j]["Number"]).ToString("0.00");
                                dr["ClassName"] = className;
                                dr["Month"] = iMonth;
                                dr["Title"] = TITLE;
                                dr["No"] = num;
                                dt.Rows.Add(dr);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            DataRow dr = dt.NewRow();
                            dr["Numbers"] = 0;
                            dr["ClassName"] = className;
                            dr["Month"] = iMonth;
                            dr["Title"] = TITLE;
                            dr["No"] = num;
                            dt.Rows.Add(dr);
                        }
                    }
                    n++;
                }
                classdt.Dispose();
                tempdt.Dispose();
            }
            return dt;
        }

        #endregion

        #region 自付费杂品明细
        public DataTable GetGroceryDetail(string pid, string AREAID, string CLASSID, DateTime DTIME)
        {
            try
            {
                CMS_CateringDataSet.DTGroceryDetailDataTable dt = new CMS_CateringDataSet.DTGroceryDetailDataTable();
                List<Catering_Model.ClassInfo> listClassInfo = null;
                if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10"))
                {
                    listClassInfo = new Catering_Rules.ClassInfo().GetModelList("").Where(p => p.id == Convert.ToInt32(CLASSID)).ToList<Catering_Model.ClassInfo>();
                }
                else
                {
                    if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
                    {
                        listClassInfo = new Catering_Rules.ClassInfo().GetModelList("").Where(p => p.AreaID == Convert.ToInt32(AREAID)).ToList<Catering_Model.ClassInfo>();
                    }
                    else
                    {
                        string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
                        if (!string.IsNullOrEmpty(strarealist))
                        {
                            listClassInfo = new Catering_Rules.ClassInfo().GetModelList(" AreaID IN (" + strarealist + ")");
                        }
                        else
                        {
                            listClassInfo = new Catering_Rules.ClassInfo().GetModelList("");
                        }
                    }

                }
                DataTable frdt = dvf.GetGroceryDetail(AREAID, CLASSID, DTIME);
                for (int i = 0; i < listClassInfo.Count; i++)
                {
                    for (int ii = 1; ii <= 12; ii++)
                    {
                        DataRow tempdr = dt.NewRow();
                        tempdr["ClassName"] = listClassInfo[i].CName;
                        tempdr["Month"] = ii;
                        DataRow[] frow = frdt.Select(string.Format(" [Month]={0} AND ClassID={1}", ii, listClassInfo[i].id));
                        decimal jine = 0;
                        decimal person = 0;
                        decimal avg = 0;
                        if (frow != null && frow.Count() > 0)
                        {

                            if (!(frow[0]["JinE"] is DBNull) && frow[0]["JinE"].ToString() != "")
                            {
                                Decimal.TryParse(frow[0]["JinE"].ToString(), out jine);
                            }
                            if (!(frow[0]["SUMPerson"] is DBNull) && frow[0]["SUMPerson"].ToString() != "")
                            {
                                Decimal.TryParse(frow[0]["SUMPerson"].ToString(), out person);
                            }
                            if (person > 0)
                            {
                                avg = Toolss.GetMathResult(jine / person, 2, 3);
                            }
                        }
                        tempdr["PersonAvg"] = avg;
                        tempdr["GroceryCount"] = jine;
                        tempdr["AteNumbers"] = person;
                        dt.Rows.Add(tempdr);
                    }
                }
                frdt.Dispose();
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region 就餐人员年/月统计
        /// <summary>
        /// 就餐人员年/月统计
        /// </summary>
        /// <param name="GROUPID">作业区ID</param>
        /// <param name="CLASSID">班组ID</param>
        /// <param name="DTIME">时间</param>
        /// <returns>DataTable</returns>
        public DataTable GetDynamicAte(string pid, string GROUPID, string CLASSID, DateTime DTIME, string TITLE)
        {
            CMS_CateringDataSet.DTAteYearOrMonthDataTable dt = new CMS_CateringDataSet.DTAteYearOrMonthDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dtDynamicAte = dvf.GetDynamicAte(strclasslist, GROUPID, CLASSID, DTIME);
            var temp = from t in dtDynamicAte.AsEnumerable()
                       select new
                       {
                           Number = Convert.ToInt32(t["number"]),
                           Eat = Convert.ToInt32(t["eat"]),
                           GetMoney = Convert.ToInt32(t["getmoney"]),
                           MofD = Convert.ToInt32(t["monthofdate"]),
                           YofD = Convert.ToInt32(t["yearofdate"]),
                           DofD = Convert.ToInt32(t["day"])
                       };
            var listOfDy = temp.ToList();
            if (!DTIME.Second.Equals(11))
            {
                for (int i = 1; i <= 31; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Date"] = i;
                    dr["Title"] = TITLE;
                    if (listOfDy.Where(p => p.DofD.Equals(i)).Count() > 0)
                    {
                        var info = listOfDy.Where(p => p.DofD.Equals(i)).First();
                        dr["AteCount"] = info.GetMoney + info.Number;//收费人数
                        dr["AteNumber"] = info.Eat;//配餐人数
                    }
                    dt.Rows.Add(dr);
                }
            }
            else
            {
                for (int i = 1; i <= 12; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Date"] = i;
                    dr["Title"] = TITLE;
                    if (listOfDy.Where(p => p.MofD.Equals(i)).Count() > 0)
                    {
                        var info = listOfDy.Where(p => p.MofD.Equals(i)).First();
                        dr["AteCount"] = info.GetMoney + info.Number;
                        dr["AteNumber"] = info.Eat;
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        public DataTable GetDynamicAte(string pid, string GROUPID, string CLASSID, DateTime DTStart, DateTime DTEnd, string TITLE)
        {
            CMS_CateringDataSet.DTAteYearOrMonthDataTable dt = new CMS_CateringDataSet.DTAteYearOrMonthDataTable();
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            DataTable dtDynamicAte = dvf.GetDynamicAte(strclasslist, GROUPID, CLASSID, DTStart, DTEnd);
            if (dtDynamicAte != null && dtDynamicAte.Rows.Count > 0)
            {
                for (int i = 0; i < dtDynamicAte.Rows.Count; i++)
                {
                    int Number = Convert.ToInt32(dtDynamicAte.Rows[i]["number"]);
                    int Eat = Convert.ToInt32(dtDynamicAte.Rows[i]["eat"]);
                    int GetMoney = Convert.ToInt32(dtDynamicAte.Rows[i]["getmoney"]);
                    string DTime = dtDynamicAte.Rows[i]["date"].ToString();
                    DataRow dr = dt.NewRow();
                    dr["Date"] = DTime;
                    dr["Title"] = TITLE;
                    dr["AteCount"] = GetMoney + Number;
                    dr["AteNumber"] = Eat;
                    dt.Rows.Add(dr);
                }
                dtDynamicAte.Dispose();
            }
            return dt;
        }

        #endregion

        #region 配餐人员动态
        public DataTable GetSetingPerson(string AREAID, string CLASSID, string IsWorking, string IsWorkText)
        {
            CMS_CateringDataSet.DTSCPDataTable dt = new CMS_CateringDataSet.DTSCPDataTable();
            DataTable dttemp = dvf.GetSetingPerson(CLASSID, IsWorking);
            var temp = from t in dttemp.AsEnumerable()
                       select new
                       {
                           id = t["id"],
                           name = t["Name"],
                           classID = Convert.ToInt32(t["ClassID"]),
                           pTID = t["PTID"],
                           cID = t["CID"],
                           pID = t["PID"],
                           eatID = t["EatID"],
                           cName = t["CName"],
                           className = t["ClassName"],
                           pName = t["PName"],
                           isWorking = t["IsWorking"],
                           areaName = t["AreaName"]
                       };
            if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10"))
            {
                foreach (var item in temp)
                {
                    DataRow dr = dt.NewRow();
                    dr["AreaName"] = item.areaName;
                    dr["ClassName"] = item.className;
                    dr["Company"] = item.cName;
                    dr["DepName"] = item.pName;
                    dr["IsWorking"] = item.isWorking;
                    dr["PName"] = item.name;
                    dr["id"] = item.id;
                    dt.Rows.Add(dr);
                }
            }
            else
            {
                Catering_Rules.OperationArea OAreaRules = new Catering_Rules.OperationArea();
                DataTable dtOArea = null;
                if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
                {
                    dtOArea = OAreaRules.GetList(string.Format(" id = {0}", AREAID)).Tables[0];
                }
                else
                {
                    dtOArea = OAreaRules.GetAllList().Tables[0];
                }
                Catering_Rules.ClassInfo ClassRules = new Catering_Rules.ClassInfo();
                DataTable dtClass = classRules.GetAllList().Tables[0];
                var classTemp = from c in dtClass.AsEnumerable()
                                select new
                                {
                                    id = Convert.ToInt32(c["id"]),
                                    cName = c["CName"].ToString(),
                                    areaID = Convert.ToInt32(c["AreaID"])
                                };
                for (int i = 0; i < dtOArea.Rows.Count; i++)
                {
                    foreach (var item in classTemp.Where(p => p.areaID == Convert.ToInt32(dtOArea.Rows[i]["id"])))
                    {
                        DataRow dr = dt.NewRow();
                        dr["AreaName"] = dtOArea.Rows[i]["AreaName"].ToString();
                        dr["ClassName"] = item.cName;
                        var info = temp.Where(p => p.classID == item.id).Count() > 0 ? temp.Where(p => p.classID == item.id).First() : null;
                        if (info != null)
                        {
                            dr["Company"] = info.cName.ToString();
                            dr["DepName"] = info.pName.ToString();
                            //dr["IsWorking"] = info.isWorking.ToString();

                            dr["PName"] = info.name;
                            dr["id"] = info.id;
                        }
                        dr["IsWorking"] = IsWorkText;
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }


        /// <summary>
        /// 获取配餐人员动态列表
        /// </summary>
        /// <param name="GroupID">作业区ID</param>
        /// <param name="ClassID">班组ID</param>
        /// <param name="DTime">时间</param>
        /// <param name="isZiYingCompany">状态：true：包含自营公司、false：不包含自营公司</param>
        /// <returns></returns>
        public DataTable GetAttendanceRecordReport(string pid, string GroupID, string ClassID, DateTime DTime, bool isZiYingCompany)
        {
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            return dvf.GetAttendanceRecordReport(strclasslist, GroupID, ClassID, DTime, isZiYingCompany);
        }

        public DataTable GetZiYingCompanyAttendanceRecord()
        {
            return dvf.GetZiYingCompanyAttendanceRecord();
        }

        /// <summary>
        /// 获取更公司人数统计
        /// </summary>
        /// <param name="GroupID"></param>
        /// <param name="ClassID"></param>
        /// <param name="DTime"></param>
        /// <returns></returns>
        public DataTable GetAttendanceRecordByMSN(string GroupID, string ClassID, DateTime DTime)
        {
            return dvf.GetAttendanceRecordByMSN(GroupID, ClassID, DTime);
        }

        /// <summary>
        /// 获取更公司人数统计
        /// </summary>
        /// <param name="DTime"></param>
        /// <returns></returns>
        public DataTable GetZiYingAttendanceRecordByMSN(DateTime DTime)
        {
            return dvf.GetZiYingAttendanceRecordByMSN(DTime);
        }

        /// <summary>
        /// 获取配餐人员动态列表
        /// </summary>
        /// <param name="GroupID">作业区ID</param>
        /// <param name="ClassID">班组ID</param>
        /// <param name="DTime">时间</param>
        /// <param name="isZiYingCompany">状态：true：包含自营公司、false：不包含自营公司</param>
        /// <returns></returns>
        public DataTable GetPeicanDongTaiByDate(string pid, string GroupID, string ClassID, DateTime DTime, bool isZiYingCompany)
        {
            string strclasslist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            return dvf.GetPeicanDongTaiByDate(strclasslist, GroupID, ClassID, DTime, isZiYingCompany);
        }

        #endregion

        #region 月考勤
        public DataTable GetWorkOfMonth(string AREAID, string CLASSID, string TEXT, DateTime DTIME)
        {
            CMS_CateringDataSet.View_AttendanceRecordDataTable dt = new CMS_CateringDataSet.View_AttendanceRecordDataTable();
            DataTable dtTemp = dvf.GetWorkOfMonth(CLASSID, TEXT, DTIME);
            var temp = from t in dtTemp.AsEnumerable()
                       select new
                       {
                           Name = t["Name"].ToString(),
                           ClassName = t["ClassName"].ToString(),
                           PName = t["PName"].ToString(),
                           Day = Convert.ToInt32(t["Date"])
                       };
            int num = 0;
            foreach (var info in temp)
            {
                for (int i = 1; i <= 31; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = ++num;
                    dr["name"] = info.Name;
                    dr["pname"] = info.PName;
                    dr["nummonth"] = i;
                    dr["classname"] = info.ClassName;
                    if (info.Day == i)
                    {
                        dr["number"] = 1;
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion

        #region 年考勤
        public DataTable GetWorkOfYear(string AREAID, string CLASSID, string TEXT, DateTime DTIME)
        {
            CMS_CateringDataSet.View_AttendanceRecordDataTable dt = new CMS_CateringDataSet.View_AttendanceRecordDataTable();
            DataTable dtTemp = dvf.GetWorkOfYear(CLASSID, TEXT, DTIME);
            var temp = from t in dtTemp.AsEnumerable()
                       select new
                       {
                           Name = t["Name"].ToString(),
                           PName = t["PName"].ToString(),
                           Day = Convert.ToInt32(t["Date"])
                       };
            int num = 0;
            foreach (var info in temp)
            {
                for (int i = 1; i <= 12; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = ++num;
                    dr["name"] = info.Name;
                    dr["pname"] = info.PName;
                    dr["nummonth"] = i;
                    if (info.Day == i)
                    {
                        dr["number"] = 1;
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion

        #region 服务比列汇总
        public DataTable GetServiceCollect(string pid, string AREAID, string CLASSID, DateTime DTIME, string Title, bool isZiYingCompany)
        {
            CMS_CateringDataSet.View_ServiceCollectDataTable dt = new CMS_CateringDataSet.View_ServiceCollectDataTable();
            DataTable dttemp = dvf.GetServiceCollect(AREAID, CLASSID, DTIME);
            string classWhere = "";
            if (!AREAID.Equals("-10"))
            {
                classWhere = " AreaID=" + AREAID + " ";
            }
            else
            {
                string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);

                if (!string.IsNullOrEmpty(strarealist))
                {
                    classWhere = string.Format(" AreaID IN ({0}) ", strarealist);
                }
            }
            if (!CLASSID.Equals("-10"))
            {
                classWhere += string.Format(" AND ID ={0} ", CLASSID);
            }
            var listClass = new Catering_Rules.ClassInfo().GetModelList(classWhere);

            int num = 0;
            foreach (var itemClass in listClass)
            {
                if (!isZiYingCompany && itemClass.CName == "自营公司") continue;
                DataRow dr = dt.NewRow();
                dr["ClassName"] = itemClass.CName;
                dr["Title"] = Title;
                for (int i = 0; i < dttemp.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dttemp.Rows[i]["ClassID"]) == itemClass.id)
                    {
                        int persons = Convert.ToInt32(dttemp.Rows[i]["Persons"]);
                        int QC = Convert.ToInt32(dttemp.Rows[i]["QC"]);
                        int Catering = Convert.ToInt32(dttemp.Rows[i]["Catering"]);
                        dr["NumOfCount"] = dttemp.Rows[i]["PersonCount"];
                        dr["OutSideNum"] = QC;
                        dr["ClassNum"] = persons;
                        dr["IntSideNum"] = Catering;
                        dr["GetCashNum"] = dttemp.Rows[i]["Number"];
                        dr["FuWuBiLi"] = Catering == 0 ? 0 : Toolss.GetMathResult(Convert.ToDecimal(persons + QC) / Convert.ToDecimal(Catering), 2, 3);
                    }
                }
                dt.Rows.Add(dr);
                num = num + 1;
            }
            //DataRow dr = dt.NewRow();
            //dr["ClassName"] = item.cName;
            // if (info != null)
            //{
            //    dr["NumOfCount"] = info.Number;
            //    dr["ClassNum"] = info.Persons;
            //    dr["OutSideNum"] = info.QC;
            //    dr["IntSideNum"] = info.Catering;
            //    dr["GetCashNum"] = Convert.ToInt32(info.Persons) + Convert.ToInt32(info.QC);//收费总人数
            //    dr["FuWuBiLi"] = "";
            //    int persons = 0;
            //    int qc = 0;
            //    int peican = 0;
            //    Int32.TryParse(info.Persons.ToString(), out persons);
            //    Int32.TryParse(info.QC.ToString(), out qc);
            //    Int32.TryParse(info.Catering.ToString(), out peican);
            //    if (peican > 0) dr["FuWuBiLi"] = ((qc + persons) / peican * 100).ToString("0.0") + "%";
            //}
            //dt.Rows.Add(dr);

            return dt;
        }
        #endregion

        #region 全年服务总表
        public DataTable GetHallYearCollect(string pid, string AREAID, string CLASSID, DateTime DTIME, string TITLE, bool isZiYingCompany)
        {
            CMS_CateringDataSet.View_HallYearCollectDataTable dt = new CMS_CateringDataSet.View_HallYearCollectDataTable();
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            DataTable dttemp = dvf.GetHallYearCollect(strarealist, AREAID, CLASSID, DTIME);
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
                {
                    if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10"))
                    {
                        for (int j = 1; j < 13; j++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["Id"] = 1;
                            dr["Title"] = TITLE;
                            dr["Month"] = j;
                            dr["ClassName"] = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(CLASSID)).CName;
                            DataRow[] temrow = dttemp.Select(" Monthofdate=" + j + " ");
                            if (temrow != null && temrow.Count() > 0)
                            {
                                dr["SetNum"] = temrow[0]["Catering"].ToString();//配餐人数
                                dr["ServiceNum"] = temrow[0]["QC"].ToString();//服务人数                                        
                            }
                            dt.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        DataTable classtab = new Catering_Rules.ClassInfo().GetList(" AreaID=" + AREAID).Tables[0];
                        for (int i = 0; i < classtab.Rows.Count; i++)
                        {
                            string className = classtab.Rows[i]["CName"].ToString();
                            int classid = Convert.ToInt32(classtab.Rows[i]["ID"]);
                            if (classid <= 0) continue;
                            for (int j = 1; j < 13; j++)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = (i + 1);
                                dr["Title"] = TITLE;
                                dr["ClassName"] = className;
                                dr["Month"] = j;
                                DataRow[] temrow = dttemp.Select(" Monthofdate=" + j + " AND ClassId=" + classid + " ");
                                if (temrow != null && temrow.Count() > 0)
                                {
                                    dr["SetNum"] = temrow[0]["Catering"].ToString();//配餐人数
                                    dr["ServiceNum"] = temrow[0]["QC"].ToString();//服务人数                                        
                                }
                                dt.Rows.Add(dr);
                            }
                        }
                        classtab.Dispose();
                    }
                }
                else
                {
                    string[] area = strarealist.Split(',');
                    if (area.Count() > 0)
                    {
                        int xuhao = 1;
                        for (int k = 0; k < area.Count(); k++)
                        {
                            int id = 0;
                            Int32.TryParse(area[k], out id);
                            if (id <= 0) continue;
                            DataTable classtab = new Catering_Rules.ClassInfo().GetList(" AreaID=" + id).Tables[0];
                            for (int i = 0; i < classtab.Rows.Count; i++)
                            {
                                string className = classtab.Rows[i]["CName"].ToString();
                                int classid = Convert.ToInt32(classtab.Rows[i]["ID"]);
                                if (classid <= 0) continue;
                                if (!isZiYingCompany && className == "自营公司") continue;
                                for (int j = 1; j < 13; j++)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["Id"] = xuhao;
                                    dr["Title"] = TITLE;
                                    dr["ClassName"] = className;
                                    dr["Month"] = j;
                                    DataRow[] temrow = dttemp.Select(" Monthofdate=" + j + " AND ClassId=" + classid + " ");
                                    if (temrow != null && temrow.Count() > 0)
                                    {
                                        dr["SetNum"] = temrow[0]["Catering"].ToString();//配餐人数
                                        dr["ServiceNum"] = temrow[0]["QC"].ToString();//服务人数                                        
                                    }
                                    dt.Rows.Add(dr);
                                }
                                xuhao++;
                            }
                            classtab.Dispose();
                        }
                    }

                }
                dttemp.Dispose();
            }
            return dt;
        }
        #endregion

        #region 人均服务比例图
        public DataTable GetPersonAvgService(string pid, string AREAID, string CLASSID, DateTime DTIME, bool isMonth)
        {
            string strarealist = new Catering_Rules.OperationArea().GetStrAreaIDListByPersonnelID(pid);
            return dvf.GetPersonAvgService(strarealist, AREAID, CLASSID, DTIME, isMonth);
        }
        #endregion

        #region 成本数据统计

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="GetPerson">收入合同人均费率</param>
        /// <param name="al">统计项：0:金额 1:采购口径（%） 2:消耗口径（%） 3:采购口径（元） 4:消耗口径（元） 5:期初库存 6:期末库存 7:本月采购 </param>
        /// <param name="AREAID"></param>
        /// <param name="DTIME1"></param>
        /// <param name="DTIME2"></param>
        /// <param name="TIMETYPE"></param>
        /// <returns></returns>
        public DataTable GetCostData(string pid, string GetPerson, ArrayList al, string AREAID, DateTime DTIME1, DateTime DTIME2, DateTime DTIME3, DateTime DTIME4)
        {
            decimal GP = Convert.ToDecimal(GetPerson);
            CMS_CateringDataSet.DTCostDataStatisticsDataTable dt = new CMS_CateringDataSet.DTCostDataStatisticsDataTable();
            string classWhere = "";
            string classidlist = "";
            if (!AREAID.Equals("-10"))
            {
                classidlist = new Catering_Rules.ClassInfo().GetStrtClassIDListByGroupID(AREAID);
            }
            else
            {
                classidlist = new Catering_Rules.ClassInfo().GetStrClassIDListByPersonnelID(pid);
            }
            if (!string.IsNullOrEmpty(classidlist)) classWhere = string.Format(" id IN ({0}) ", classidlist);
            List<Catering_Model.E_RawMaterials> listRaws = new Catering_Rules.T_RawMaterials().GetModelList("");
            List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetModelList(classWhere);

            if (!AREAID.Equals("-10"))
            {
                listClass = listClass.Where(p => p.AreaID == Convert.ToInt32(AREAID)).ToList<Catering_Model.ClassInfo>();
            }


            foreach (Catering_Model.ClassInfo itemClass in listClass)
            {
                //本班组出库总金额合计
                decimal Parent_Ck = new Catering_Rules.FromRecords().GetAmountMoneyByCLASS(itemClass.id.ToString(), 0, DTIME1, DTIME2);
                //本班组入库总金额合计
                decimal Parent_Ruku = new Catering_Rules.DataViewForReport().GetProcureCountByClass(itemClass.id.ToString(), 0, DTIME1, DTIME2);
                //本班组就餐人数
                int Catering = 0;
                DataTable dtDynamic = new Catering_Rules.Dynamic().GetCateringAndEatNumber("-10", itemClass.id, DTIME1, DTIME2);
                if (dtDynamic != null && dtDynamic.Rows.Count > 0)
                {
                    if (!(dtDynamic.Rows[0]["Catering"] is DBNull) && !string.IsNullOrEmpty(dtDynamic.Rows[0]["Catering"].ToString())) Catering = Convert.ToInt32(dtDynamic.Rows[0]["Catering"].ToString());
                    dtDynamic.Dispose();
                }
                foreach (Catering_Model.E_RawMaterials itemRaw in listRaws)
                {
                    //本班组原料大类消耗金额合计
                    decimal Child_Ck = new Catering_Rules.FromRecords().GetAmountMoneyByCLASS(itemClass.id.ToString(), itemRaw.id, DTIME1, DTIME2);
                    //本班组原料大类入库金额合计
                    decimal Child_Ruku = new Catering_Rules.DataViewForReport().GetProcureCountByClass(itemClass.id.ToString(), itemRaw.id, DTIME1, DTIME2);
                    decimal First_KC = Toolss.GetMathResult(new Catering_Rules.DataViewForReport().GetCountResultByClass(DTIME3, itemRaw.id, itemClass.id), 2, 3);
                    decimal Last_KC = Toolss.GetMathResult(new Catering_Rules.DataViewForReport().GetCountResultByClass(DTIME4, itemRaw.id, itemClass.id), 2, 3);

                    for (int i = 0; i < al.Count; i++)
                    {
                        DataRow dr = GetCostRow(al[i].ToString(), GP, itemClass.CName, itemRaw.TypeName, Child_Ruku, Catering, First_KC, Last_KC, Child_Ck, Parent_Ruku, Parent_Ck);
                        if (dr != null)
                        {
                            dt.Rows.Add(dr.ItemArray);
                        }
                    }
                    #region old

                    //DataTable dttemp = dvf.GetCostData(itemClass.id.ToString(), itemRaw.id.ToString(), TIMETYPE, DTIME1, DTIME2);

                    //if (dttemp != null && dttemp.Rows.Count > 0)
                    //{
                    //    // <param name="Money">本月采购（入库金额）</param>
                    //    // <param name="EatNumber">就餐人数</param>
                    //    // <param name="FirstResult">期初库存</param>
                    //    // <param name="LastResult">期末库存</param>
                    //    // <param name="UsedMoney">金额（出库金额）</param>
                    //    // <param name="CaigouKoujing">采购口径（元）（入库金额/）</param>
                    //    // <param name="XiaohaoKoujing">消耗口径（元）</param>

                    //    decimal Money = FormatDecimal(dttemp.Rows[0]["AmountMoney"]);
                    //    decimal FirstResult = FormatDecimal(dttemp.Rows[0]["FirstResult"]);
                    //    decimal LastResult = FormatDecimal(dttemp.Rows[0]["LastResult"]);
                    //    decimal EatNumber = FormatDecimal(dttemp.Rows[0]["EatNumber"]);
                    //    decimal CaiGouKouJing = FormatDecimal(dttemp.Rows[0]["CaiGouKouJing"]);
                    //    decimal XiaoHaoKouJing = FormatDecimal(dttemp.Rows[0]["XiaoHaoKouJing"]);
                    //    decimal UsedMoney = FormatDecimal(dttemp.Rows[0]["UsedMoney"]);
                    //    if (Money == 0 && FirstResult == 0 && LastResult == 0 && EatNumber == 0 && CaiGouKouJing == 0 && XiaoHaoKouJing == 0 && UsedMoney == 0)
                    //    {
                    //        continue;
                    //    }
                    //    for (int i = 0; i < al.Count; i++)
                    //    {
                    //        DataRow dr = GetCostRow(al[i].ToString(), GP, itemClass.CName, itemRaw.TypeName, Money, EatNumber, FirstResult, LastResult, UsedMoney, CaiGouKouJing, XiaoHaoKouJing);
                    //        if (dr != null)
                    //        {
                    //            dt.Rows.Add(dr.ItemArray);
                    //        }
                    //    }

                    //}
                    #endregion
                }
            }
            return dt;
        }
        /// <summary>
        /// 转换类型
        /// </summary>
        /// <param name="dc"></param>
        /// <returns></returns>
        public decimal FormatDecimal(object obj)
        {
            try
            {
                if (Convert.ToDecimal(obj) == 0)
                {
                    return 0;
                }
                decimal temp = 0;
                if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
                {
                    if (obj.ToString().IndexOf(".") == -1)
                    {
                        temp = Convert.ToDecimal(obj.ToString());
                    }
                    else
                    {
                        temp = Convert.ToDecimal(Convert.ToDecimal(obj).ToString("F2"));
                        if (temp.ToString().IndexOf(".") != -1)
                        {
                            if (temp > 8000000)
                            {
                                temp = Convert.ToDecimal(Convert.ToDouble(temp.ToString("G7")));
                            }
                            else
                            {
                                temp = Convert.ToDecimal(temp.ToString("G7"));
                            }

                        }
                    }
                }
                else
                {
                    temp = 0;
                }
                return temp;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 根据参数返回DataRow
        /// </summary>
        /// <param name="al">项目类型</param>
        /// <param name="GetPerson">收入合同人均费率</param>
        /// <param name="CName">班组名称</param>
        /// <param name="TypeName">原料大类名称</param>
        /// <param name="Money">本月采购金额（入库）</param>
        /// <param name="EatNumber">就餐人数</param>
        /// <param name="FirstResult">期初库存</param>
        /// <param name="LastResult">期末库存</param>
        /// <param name="UsedMoney">本月消耗金额（出库）</param>
        /// <param name="Zong_Ruku">总入库</param>
        /// <param name="Zong_Chuk">总入出库</param>
        /// <returns></returns>
        private DataRow GetCostRow(string al, decimal GetPerson, string CName, string TypeName, decimal Money, decimal EatNumber, decimal FirstResult, decimal LastResult, decimal UsedMoney, decimal Zong_Ruku, decimal Zong_Chuk)
        {
            DataRow dr = new CMS_CateringDataSet.DTCostDataStatisticsDataTable().NewRow();
            try
            {
                switch (al)
                {
                    case "0"://金额
                        AddResultRow(out dr, CName, TypeName, "金额", UsedMoney, EatNumber);
                        //dr["RawPercent"] = Convert.ToDecimal(am) / GetPerson;
                        break;
                    case "1"://采购口径（%）=本月采购/总入库合计
                        AddResultRow(out dr, CName, TypeName, "采购口径（%）", Toolss.GetMathResult(Money / Zong_Ruku, 2, 3), EatNumber);
                        //dr["RawPercent"] = CaigouKoujing / GetPerson;//食品原材料采购
                        break;
                    case "2"://消耗口径（%）=本月出库/总出库合计
                        AddResultRow(out dr, CName, TypeName, "消耗口径（%）", Toolss.GetMathResult(UsedMoney / Zong_Chuk, 2, 3), EatNumber);
                        //dr["RawPercent"] = XiaohaoKoujing / GetPerson;//食品原材料消耗
                        break;
                    case "3"://采购口径（元）
                        AddResultRow(out dr, CName, TypeName, "采购口径（元）", Toolss.GetMathResult(Money / EatNumber, 2, 3), EatNumber);
                        break;
                    case "4"://消耗口径（元）
                        AddResultRow(out dr, CName, TypeName, "消耗口径（元）", Toolss.GetMathResult(UsedMoney / EatNumber, 2, 3), EatNumber);
                        break;
                    case "5"://期初库存
                        AddResultRow(out dr, CName, TypeName, "期初库存", FirstResult, EatNumber);
                        break;
                    case "6"://期末库存
                        AddResultRow(out dr, CName, TypeName, "期末库存", LastResult, EatNumber);
                        break;
                    case "7"://本月采购
                        AddResultRow(out dr, CName, TypeName, "本月采购", Money, EatNumber);
                        break;
                    default:
                        dr = null;
                        break;
                }
            }
            catch (Exception)
            {
                dr = null;
            }
            return dr;
        }


        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="dr">要返回的DataRow</param>
        /// <param name="CName">班组名</param>
        /// <param name="TypeName">原来大类名称</param>
        /// <param name="ProjectType">项目名称</param>        
        /// <param name="Result">具体值</param>
        /// <param name="EatNumbers">就餐人数</param>
        /// 
        private void AddResultRow(out DataRow dr, string CName, string TypeName, string ProjectType, decimal Result, decimal EatNumbers)
        {
            dr = new CMS_CateringDataSet.DTCostDataStatisticsDataTable().NewRow();
            dr["ProjectType"] = ProjectType;
            dr["ClassName"] = CName;
            dr["RawTId"] = TypeName;
            dr["Money"] = Result;
            dr["EatNumbers"] = EatNumbers;
        }

        #endregion

        #region 原料成本统计

        public DataTable GetYuanLiaoChengBen(int CompanyID, int AREAID, int CLASSID, int[] RAWTID, int Year)
        {
            DataTable tempTable = new DataTable();
            tempTable.Columns.Add("RAWTID", typeof(int));//增加字段RAWTID
            tempTable.Columns.Add("RAWName", typeof(string));//增加字段RAWName
            tempTable.Columns.Add("AmountMoney", typeof(int));//增加字段AmountMoney
            tempTable.Columns.Add("ChargePerson", typeof(int));//增加字段ChargePerson
            tempTable.Columns.Add("MonthDay", typeof(int));//增加字段MonthDay
            tempTable.Columns.Add("Persons", typeof(int));//增加字段Persons(实际就餐人数或收费人数=定员+其他服务人员)
            tempTable.Columns.Add("Catering", typeof(int));//增加字段Catering(配餐服务人数=配餐在岗人员)
            DataTable dt = new Catering_Rules.FromRecords().GetYuanLiaoChengBenGroupByMonthAndRAWTID(CompanyID, AREAID, CLASSID, RAWTID, Year);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 1; i <= 12; i++)//月份
                {
                    for (int j = 0; j < RAWTID.Length; j++)
                    {
                        if (RAWTID[j] == -1) continue;
                        DataRow dr = tempTable.NewRow();
                        dr["RAWTID"] = RAWTID[j];
                        string rawname = "";
                        for (int n = 0; n < dt.Rows.Count; n++)
                        {
                            int temprawtid = Convert.ToInt32(dt.Rows[n]["RAWTID"]);
                            int tempmonth = Convert.ToInt32(dt.Rows[n]["MonthDay"]);
                            if (i == tempmonth && RAWTID[j] == temprawtid)
                            {
                                dr["AmountMoney"] = Convert.ToInt32(dt.Rows[n]["AmountMoney"]);
                                break;
                            }
                        }
                        Catering_Model.E_RawMaterials rm = new Catering_Rules.T_RawMaterials().GetModel(RAWTID[j]);
                        if (rm != null && rm.TypeName != "")
                        {
                            rawname = rm.TypeName;
                        }
                        dr["RAWName"] = rawname;
                        dr["MonthDay"] = i;
                        int QC = 0, Persons = 0, Catering = 0;
                        new Catering_Rules.Dynamic().GetEatNumberByClassIDAndMonth(CLASSID, Year, i, out QC, out Persons, out Catering);
                        dr["Persons"] = Persons + QC;
                        dr["Catering"] = Catering;
                        dr["ChargePerson"] = new Catering_Rules.ChargePersonNumber().GetChargePersonNumberByClassIDAndDate(CLASSID, Year, i);
                        tempTable.Rows.Add(dr);
                    }
                }
                dt.Dispose();
            }
            return tempTable;
        }

        #endregion

        #region 人均成本/就餐人数月度走势

        public DataTable GetChengbenRenshuYueduZoushi(int CompanyID, int AREAID, int CLASSID, int Year)
        {
            DataTable tempTable = new DataTable();
            tempTable.Columns.Add("AmountMoney", typeof(decimal));//增加字段AmountMoney
            tempTable.Columns.Add("MonthDay", typeof(int));//增加字段MonthDay
            tempTable.Columns.Add("Number", typeof(int));//增加字段Number(就餐人数或收费人数=定员+其他服务人员)
            DataTable dt = new Catering_Rules.FromRecords().GetChengBenGroupByMonth(CompanyID, AREAID, CLASSID, Year);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 1; i <= 12; i++)//月份
                {
                    DataRow dr = tempTable.NewRow();
                    dr["MonthDay"] = i;
                    for (int n = 0; n < dt.Rows.Count; n++)
                    {
                        if (i == Convert.ToInt32(dt.Rows[n]["MonthDay"]))
                        {
                            dr["AmountMoney"] = Convert.ToInt32(dt.Rows[n]["AmountMoney"]);
                        }
                    }
                    int QC = 0, Persons = 0, Catering = 0;
                    new Catering_Rules.Dynamic().GetEatNumberByClassIDAndMonth(CLASSID, Year, i, out QC, out Persons, out Catering);
                    dr["Number"] = Persons + QC;
                    tempTable.Rows.Add(dr);
                }
                dt.Dispose();
            }
            return tempTable;
        }

        #endregion

        #region 各类原材料人均成本走势图

        public DataTable GetRenJunChengBenZouShi(int CompanyID, int AREAID, int CLASSID, int[] RAWTID, int Year)
        {
            DataTable tempTable = new DataTable();
            tempTable.Columns.Add("RAWTID", typeof(int));//增加字段RAWTID
            tempTable.Columns.Add("RAWName", typeof(string));//增加字段RAWName
            tempTable.Columns.Add("AmountMoney", typeof(decimal));//增加字段AmountMoney
            tempTable.Columns.Add("MonthDay", typeof(int));//增加字段MonthDay
            tempTable.Columns.Add("Number", typeof(int));//增加字段Persons(实际就餐人数或收费人数=定员+其他服务人员)
            DataTable dt = new Catering_Rules.FromRecords().GetYuanLiaoChengBenGroupByMonthAndRAWTID(CompanyID, AREAID, CLASSID, RAWTID, Year);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 1; i <= 12; i++)//月份
                {
                    for (int j = 0; j < RAWTID.Length; j++)
                    {
                        if (RAWTID[j] == -1) continue;
                        DataRow dr = tempTable.NewRow();
                        dr["RAWTID"] = RAWTID[j];
                        string rawname = "";
                        for (int n = 0; n < dt.Rows.Count; n++)
                        {
                            int temprawtid = Convert.ToInt32(dt.Rows[n]["RAWTID"]);
                            int tempmonth = Convert.ToInt32(dt.Rows[n]["MonthDay"]);
                            if (i == tempmonth && RAWTID[j] == temprawtid)
                            {
                                dr["AmountMoney"] = Convert.ToInt32(dt.Rows[n]["AmountMoney"]);
                                break;
                            }
                        }
                        Catering_Model.E_RawMaterials rm = new Catering_Rules.T_RawMaterials().GetModel(RAWTID[j]);
                        if (rm != null && rm.TypeName != "")
                        {
                            rawname = rm.TypeName;
                        }
                        dr["RAWName"] = rawname;
                        dr["MonthDay"] = i;
                        int QC = 0, Persons = 0, Catering = 0;
                        new Catering_Rules.Dynamic().GetEatNumberByClassIDAndMonth(CLASSID, Year, i, out QC, out Persons, out Catering);
                        dr["Number"] = Persons + QC;
                        tempTable.Rows.Add(dr);
                    }
                }
                dt.Dispose();
            }
            return tempTable;
        }

        #endregion

        #region 日报统计

        /// <summary>
        /// 日报统计
        /// </summary>
        /// <param name="StrCompanyID">公司ID</param>
        /// <param name="StrGroupID">作业区</param>
        /// <param name="StrClassID">班组</param>
        /// <param name="StartDate">开始时间</param>
        /// <param name="EndDate">结束时间</param>
        /// <param name="DateType">时间类型</param>
        /// <param name="isContainZiYing">是否包含自营公司</param>
        /// <returns></returns>
        public DataTable GetRbaoTongji(string StrCompanyID, string StrGroupID, string StrClassID, DateTime StartDate, DateTime EndDate, string DateType, bool isContainZiYing)
        {
            DataTable result = new DataTable();
            result.Columns.Add("Day", typeof(string));
            result.Columns.Add("Shoufei", typeof(int));
            result.Columns.Add("Peican", typeof(int));
            result.Columns.Add("Danwei", typeof(string));
            result.Columns.Add("Shoufei_Z", typeof(int));
            result.Columns.Add("DanweiNumber", typeof(int));
            result.Columns.Add("Peican_Z", typeof(int));



            //日报人员总数数据
            DataTable dt = dvf.GetView_RiBaoData(StrCompanyID, StrGroupID, StrClassID, StartDate, EndDate, DateType, isContainZiYing);
            //其他单位人员列表
            DataTable dt_qt = dvf.GetRiBaoData_Other(StrCompanyID, StrGroupID, StrClassID, StartDate, EndDate, DateType, isContainZiYing);
            if (dt == null || dt.Rows.Count == 0) return null;
            var data = from t in dt.AsEnumerable()
                       group t by new { t1 = t.Field<DateTime>("Date") } into m
                       select new
                       {
                           date = m.Key.t1,
                           dingyan = m.Sum(n => n.Field<int>("DingYuan")),
                           qita = m.Sum(n => n.Field<int>("QiTa")),
                           peicanzaigang = m.Sum(n => n.Field<int>("PeiCanZaiGang"))
                       };

            var qitadw = from t in dt_qt.AsEnumerable()
                         group t by new { t1 = t.Field<DateTime>("Dates"), t2 = t.Field<string>("temp1") } into m
                         select new
                         {
                             date = m.Key.t1,
                             danwei = m.Key.t2,
                             person = m.Sum(n => n.Field<int>("Eat"))
                         };
            dt.Dispose();
            dt_qt.Dispose();

            if (DateType == "Year")
            {
                var newdata = from t in data
                              group t by new { t1 = t.date.Month } into m
                              select new
                              {
                                  date = m.Key.t1,
                                  dingyuan = m.Sum(n => n.dingyan),
                                  qita = m.Sum(n => n.qita),
                                  peicanzaigang = m.Sum(n => n.peicanzaigang)
                              };
                var newqitadw = from t in qitadw
                                group t by new { t1 = t.date.Month, t2 = t.danwei } into m
                                select new
                                {
                                    date = m.Key.t1,
                                    danwei = m.Key.t2,
                                    person = m.Sum(n => n.person)
                                };
                for (int i = 0; i < 12; i++)
                {
                    bool hasRow = false;
                    var d_i = newdata.Where(o => o.date == (i + 1));
                    newqitadw.Where(o => o.date == (i + 1)).ToList().ForEach(n =>
                    {
                        DataRow dr = result.NewRow();
                        dr["Day"] = n.date.ToString("00") + "月";
                        dr["Shoufei"] = (d_i.Sum(o => o.dingyuan) + d_i.Sum(o => o.qita));
                        dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                        dr["Danwei"] = n.danwei;
                        dr["DanweiNumber"] = n.person;
                        dr["Shoufei_Z"] = (newdata.Sum(o => o.dingyuan) + newdata.Sum(o => o.qita));
                        dr["Peican_Z"] = newdata.Sum(o => o.peicanzaigang);
                        result.Rows.Add(dr);
                        hasRow = true;
                    });
                    if (!hasRow)
                    {
                        DataRow dr = result.NewRow();
                        dr["Day"] = (i + 1).ToString("00") + "月";
                        dr["Shoufei"] = (d_i.Sum(o => o.dingyuan) + d_i.Sum(o => o.qita));
                        dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                        dr["Shoufei_Z"] = (newdata.Sum(o => o.dingyuan) + newdata.Sum(o => o.qita));
                        dr["Peican_Z"] = newdata.Sum(o => o.peicanzaigang);
                        result.Rows.Add(dr);
                    }
                }
            }
            else if (DateType == "Month")
            {
                int maxday = (StartDate.Month == 1 || StartDate.Month == 3 || StartDate.Month == 5 || StartDate.Month == 7 || StartDate.Month == 8 || StartDate.Month == 10 || StartDate.Month == 12) ? 31 : 30;

                for (int i = 0; i < maxday; i++)
                {
                    bool hasRow = false;
                    var d_i = data.Where(o => o.date.Day == (i + 1));
                    qitadw.Where(o => o.date.Day == (i + 1)).ToList().ForEach(n =>
                    {
                        DataRow dr = result.NewRow();
                        dr["Day"] = (i + 1).ToString("00") + "日";
                        dr["Shoufei"] = (d_i.Sum(o => o.dingyan) + d_i.Sum(o => o.qita));
                        dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                        dr["Danwei"] = n.danwei;
                        dr["DanweiNumber"] = n.person;
                        dr["Shoufei_Z"] = (data.Sum(o => o.dingyan) + data.Sum(o => o.qita));
                        dr["Peican_Z"] = data.Sum(o => o.peicanzaigang);
                        result.Rows.Add(dr);
                        hasRow = true;
                    });
                    if (!hasRow)
                    {
                        DataRow dr = result.NewRow();
                        dr["Day"] = (i + 1).ToString("00") + "日";
                        dr["Shoufei"] = (d_i.Sum(o => o.dingyan) + d_i.Sum(o => o.qita));
                        dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                        dr["Shoufei_Z"] = (data.Sum(o => o.dingyan) + data.Sum(o => o.qita));
                        dr["Peican_Z"] = data.Sum(o => o.peicanzaigang);
                        result.Rows.Add(dr);
                    }
                }
            }
            else
            {
                if (StartDate < EndDate)
                {
                    for (DateTime i = StartDate.Date; i <= EndDate.Date; i = i.AddDays(1))
                    {
                        bool hasRow = false;
                        var d_i = data.Where(o => o.date == i);
                        qitadw.Where(o => o.date == i).ToList().ForEach(n =>
                        {
                            DataRow dr = result.NewRow();
                            dr["Day"] = i.ToString("yyyy年MM月dd日");
                            dr["Shoufei"] = (d_i.Sum(o => o.dingyan) + d_i.Sum(o => o.qita));
                            dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                            dr["Danwei"] = n.danwei;
                            dr["DanweiNumber"] = n.person;
                            dr["Shoufei_Z"] = (data.Sum(o => o.dingyan) + data.Sum(o => o.qita));
                            dr["Peican_Z"] = data.Sum(o => o.peicanzaigang);
                            result.Rows.Add(dr);
                            hasRow = true;
                        });
                        if (!hasRow)
                        {
                            DataRow dr = result.NewRow();
                            dr["Day"] = i.ToString("yyyy年MM月dd日");
                            dr["Shoufei"] = (d_i.Sum(o => o.dingyan) + d_i.Sum(o => o.qita));
                            dr["Peican"] = d_i.Sum(o => o.peicanzaigang);
                            dr["Shoufei_Z"] = (data.Sum(o => o.dingyan) + data.Sum(o => o.qita));
                            dr["Peican_Z"] = data.Sum(o => o.peicanzaigang);
                            result.Rows.Add(dr);
                        }
                    }
                }
            }
            return result;
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 数字处理
        /// </summary>
        /// <param name="old">要处理的数字</param>
        /// <param name="No">保留小数点位数</param>
        /// <param name="type">1：全部舍入、2：全部进1、3：四舍五入</param>
        /// <returns></returns>
        public decimal GetMathResult(decimal old, int No, int type)
        {
            decimal tempNum = 1;
            if (type == 3) return GetRound(old, No);
            if (No > 0) tempNum = Convert.ToInt32(Math.Pow(10, No));
            old = old * tempNum;
            old = Math.Truncate(old);
            old = old / tempNum;
            if (type == 1) return old;
            return old + 1 / tempNum;
        }

        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="dblnum"></param>
        /// <param name="numberprecision"></param>
        /// <returns></returns>
        private decimal GetRound(decimal dblnum, int numberprecision)
        {
            int tmpNum = dblnum > 0 ? 5 : -5;
            decimal dblreturn = Math.Truncate(dblnum * Convert.ToDecimal(Math.Pow(10, numberprecision + 1))) + tmpNum;
            dblreturn = Math.Truncate(dblreturn / 10) / Convert.ToDecimal(Math.Pow(10, numberprecision));
            return dblreturn;
        }


        #endregion
    }
}